0
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)
有什麼漂亮的方式做到這一點?
似乎我'current_user.projects'應該工作。 – MurifoX
它給了我所有的項目。我希望通過應用範圍獲得所有項目:current_assignments。 –