0

这是三个模型:

class Swimming::Classschedule < ActiveRecord::Base
  belongs_to :swimming_classtimes ,:class_name=>'Swimming::Classtime',:foreign_key => "classtime_id"  
  attr_accessible :id,:coach_id, :level_id, :note, :classtime_id
end


class Swimming::Classtime < ActiveRecord::Base
  has_many :swimming_classschedules,:class_name=>'Swimming::Classschedule'
  belongs_to :swimming_timeblocks ,:class_name=>'Swimming::Timeblock',:foreign_key => "timeblock_id"    
  attr_accessible :date, :end, :start,:timeblock_id,:id
end


class Swimming::Timeblock < ActiveRecord::Base
  has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
  has_many :swimming_classschedules,:class_name=>'Swimming::Classschedule',:through => :swimming_classtimes
  attr_accessible :name,:id
end 

我尝试加入这三个模型

schedules = Swimming::Classschedule.joins(:swimming_classtimes=>:swimming_timeblocks).select("swimming_classtimes.*,swimming_timeblocks.*,swimming_classschedules.*").where("swimming_classtimes.date >= '2013-06-01' and swimming_classtimes.date <= '2013-06-08' ")

这是返回的结果

=> [#<Swimming::Classschedule id: 7, coach_id: nil, level_id: nil, note: nil, created_at: "2013-06-08 23:24:31", updated_at: "2013-06-08 23:24:31", lesson_id: 1, classtime_id: 24, lane_id: nil, program_id: nil, classtype_id: nil>]

为什么它只返回来自 Swing_classschedules 的字段。我希望从三个模型中返回所有字段

"swimming_classtimes.*,swimming_timeblocks.*,swimming_classschedules.*"

什么不见​​了?

4

1 回答 1

1

Swimming::Classschedule 具有仅为其 db 列定义的属性。您可以使用访问其他字段

对象[:column_name]

例如:

schedules.first[:timeblock_id]
于 2013-06-09T04:13:16.390 回答