rails models has_many belongs_to relation issue

时间:2013-06-08 18:14:28

标签: ruby-on-rails ruby ruby-on-rails-3

这是我的三个模特/桌子

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'
  attr_accessible :name,:id
end


swimming_classschedules belongs_to  swimming_classtimes 
swimming_classtimes belongs_to swimming_timeblocks
swimming_timeblocks has_many swimming_classtimes
swimming_classtimes has_many swimming_classschedules

swim_classschedules和swimming_timeblocks如何关联?

1 个答案:

答案 0 :(得分:1)

swim_timeblocks has_many swimming_classschedules through swimming_classtimes

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