0

我有:

class Student < ActiveRecord::Base
    #attr_accessible :lastname, :name
    has_many :together
    has_many :teachers, :through => :together
end

class Teacher < ActiveRecord::Base
    #attr_accessible :lastname, :name
    has_many :together
    has_many :students, :through => :together
end

class Together < ActiveRecord::Base
  #attr_accessible :summary
  belongs_to :student
  belongs_to :teacher
end

我想做类似的事情:

Student.find(1).together.summary

我想访问联接表中“摘要”列中的数据...

4

1 回答 1

1

如果你只是想得到孩子,你可以这样做:

Student.find(1).teachers

如果您的教师模型有一个汇总字段,您可以执行以下操作:

Student.find(1).teachers.first.summary

我想如果您在连接表中有一个汇总字段并且您知道学生 ID,那么您可以这样做:

Together.find_by_student_id(1).summary

还有其他方法可以做到这一点。给 Rails 猫换皮的几种方法。

于 2012-12-16T21:40:41.830 回答