class User < ActiveRecord::Base
has_many :assignments
has_many :projects, through: :assignments
end
class Project < ActiveRecord::Base
has_many :assignments
has_many :users, through: :assignments
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :project
scope :current_assignments, where(...)
end
我想获取当前分配给当前用户的所有项目。就像是:
current_user.current_assigned_projects
但现在,我必须这样做:
current_user.assignments.current_assignments.map{ |a| a.project }.uniq
或者
Project.includes(:users, :assignments).where('assignments.status' => 1, 'users.id' => current_user.id)
有什么漂亮的方法可以做到这一点吗?