我有以下内容:
class Group < ActiveRecord::Base
has_many :group_projects, dependent: :destroy
has_many :projects, through: :group_projects
end
class Projects < ActiveRecord::Base
has_many :group_projects, dependent: :destroy
has_many :groups, through: :group_projects
has_many :time_entries
end
class TimeEntry < ActiveRecord::Base
belongs_to :project
end
因此,project.time_entries
返回ActiveRecord::Associations::CollectionProxy
属于该项目的时间条目。
我想要的是与特定组关联的所有项目关联的所有 time_entries 列表作为单个集合,而无需执行以下操作:
TimeEntry.where(["project_id IN (?)", @group.projects.map{|p| p.id } ])
PS:使用 Rails 4.0.0 | 红宝石 2.0.0