2012-10-01 63 views
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) 

有什麼漂亮的方式做到這一點?

+1

似乎我'current_user.projects'應該工作。 – MurifoX

+0

它給了我所有的項目。我希望通過應用範圍獲得所有項目:current_assignments。 –

回答

0

喜歡的東西

class User < ActiveRecord::Base 
    has_many :assignments 
    has_many :projects, through: :assignments 
    has_many :current_projects, conditions: 'assignments.status = 1', through: :assignments 
end 

應該工作。或者至少我希望如此;)

相關問題