我有 3 种不同的型号
class GroupMember < ActiveRecord::Base
attr_accessible :group_id, :user_id, :owner_id
has_one :user
end
class Group < ActiveRecord::Base
attr_accessible :name, :owner, :permission
has_many :groupMembers
end
class User < ActiveRecord::Base
end
当我在groups_controller.rb中时,我想通过关联实现以下查询
SELECT * FROM groups
LEFT JOIN group_members ON groups.id = group_members.group_id
LEFT JOIN users ON group_members.user_id = users.id WHERE users.id = 1
或连接查询
Group.joins('LEFT JOIN group_members ON groups.id = group_members.group_id LEFT JOIN users ON group_members.user_id = users.id').where('users.id = ?', current_user.id );
这可能吗?
Group.joins(:user_id => current_user.id)