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)

有什么漂亮的方法可以做到这一点吗?

4

1 回答 1

0

就像是

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

应该管用。或者至少我希望如此;)

于 2012-10-01T17:55:22.667 回答