Is it possible to get the ‘other’ end of an associations? For example if
teacher has_many students, it could be very readable to say something
like this:
ateacher.students[0].classes
which would call a method on the student that would list classes of the
teacher that student goes to
Could you please provide an example and explain how this corresponds to
my question? For example how to get to the teacher object in classes
method of student.
s.teacher
NoMethodError: undefined method teacher' for #<Student:0xb71ba4d8> from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:inmethod_missing’
from (irb):5
from :0
Can you please provide your own failed attempt first?
class Teacher < ActiveRecord::Base
has_many :connections
has_many :students, :through => :connections
end
class Student < ActiveRecord::Base
has_many :connections
has_many :teachers, :through => :connections
end
class Connection < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
end
t=Teacher.find_first
=> #<Teacher:0xb708d2e0 @attributes={“name”=>“Arkadiusz Jankowski”,
“id”=>“0”}
s=Teacher.find_first.students.first
=> #<Student:0xb71ba4d8 @attributes={“name”=>“Dawid Michalski”,
“id”=>“3”}
s.teacher
NoMethodError: undefined method teacher' for #<Student:0xb71ba4d8> from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in method_missing’
from (irb):5
from :0
Student doesn’t have a singular teacher, so that’s your problem. If you
want to get the connection object between a teacher and student, you can
do something like
This uses the fact I discovered that has_and_belongs_to_many
automagically adds columns from association table into the result of
association call. Seems to me it is done so by omission, but proves
helpful. The problems are that you have to perform additional
unnecessary query to that database and that it can be used only with
has_and_belongs_to_many. I’d really see a methods like teacher and
student added to the object by ActiveRecord like students and teachers
collections are. If someone has any ideas, I’d be grateful to know.
class Teacher < ActiveRecord::Base
has_and_belongs_to_many :students
def student
if @attributes[:student_id]
raise “Teacher#student requires a call through association”
end
Student.find(student_id)
end
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :teachers
def teacher
if @attributes[:teacher_id]
raise “Student#teacher requires a call through association”
end
Teacher.find(teacher_id)
end
end
sorry it should read:
class Teacher < ActiveRecord::Base
has_and_belongs_to_many :students
def student
unless @attributes[‘student_id’]
raise “Teacher#student requires a call through association”
end
Student.find(student_id)
end
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :teachers
def teacher
puts @attributes.inspect
unless @attributes[‘teacher_id’]
raise “Student#teacher requires a call through association”
end
Teacher.find(teacher_id)
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.