سؤال

I have:

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

I wanna do something like:

Student.find(1).together.summary

I want to access the data in the "summary" column, in the join table...

هل كانت مفيدة؟

المحلول

If you're just trying to get the children you can do:

Student.find(1).teachers

If your teachers model has a summary field, you could do something like this:

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

I guess if you have a summary field in the join table and you know the student id you could so this:

Together.find_by_student_id(1).summary

There's other ways to do this. A few ways to skin a Rails cat.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top