我有3个表:users
,meeting_types
和user_meeting_types
现在我想在users
表格上应用查询并在user_meeting type
中传递条件
User
有很多会议,会议有一个meeting_type
如何在user和user_meeting_type上应用条件并从所有表中获取数据?
我想要所有表格中的数据
查询是这样的:
Select * from users left join user_meeting_types on user.id = user_meeting_types.user_id left join meeting_types on user_meeting_types.meeting_id = meeting_type.id where user.id = 37 and user_meeting_type.meeting_id = 5
user.rb
class User
has_many :UserMeetingType
user_meeting_type.rb
class UserMeetingType
belongs_to :UserMeeting
答案 0 :(得分:0)
模型如下:
class User < ActiveRecord::Base
has_many :user_meeting_types
end
class MeetingType < ActiveRecord::Base
end
class UserMeetingType < ActiveRecord::Base
belongs_to :meeting_type
end
首先,您必须获取用户:
user = User.find(37) # Select * from users where id = 37
然后恢复特定用户user_meeting_types
meetings = user.user_meeting_types.where(meeting_type_id:7) #Select * from user_meeting_types where user_id = 37 and meeting_type_id = 7