Jack Christensen
2006-Apr-20 22:54 UTC
[Rails] has_many :through with has_many/has_many join models
It seems that using a join model that joins with two has_many''s will
fail to generate proper SQL
class StudentSemesterRecord < ActiveRecord::Base
belongs_to :semester
has_many :discipline_records, :through => :semester
end
class Semester < ActiveRecord::Base
has_many :student_semester_records
has_many :discipline_records
end
class DisciplineRecord < ActiveRecord::Base
belongs_to :semester
end
student_semester_record.discipline_records yields this SQL:
SELECT discipline_records.* FROM discipline_records INNER JOIN semesters
ON discipline_records.semester_id = semesters.id WHERE
(semesters.semester_id = 1349)
The where clause is invalid. It is using:
semesters.semester_id = #{student_semester_record.id}
It should be using:
semesters.id = #{student_semester_record.semester_id}
Am I doing something wrong, is this a bug, or is this association type
simply not supported?
Thanks!
--
Jack Christensen
jackc@hylesanderson.edu
Josh Susser
2006-Apr-20 23:15 UTC
[Rails] Re: has_many :through with has_many/has_many join models
Jack Christensen wrote:> It seems that using a join model that joins with two has_many''s will > fail to generate proper SQL > > class StudentSemesterRecord < ActiveRecord::Base > belongs_to :semester > has_many :discipline_records, :through => :semester > end > > class Semester < ActiveRecord::Base > has_many :student_semester_records > has_many :discipline_records > end > > class DisciplineRecord < ActiveRecord::Base > belongs_to :semester > endYou can do a has_many :through two ways. One is using a join model with two belongs_to associations. The other is to go through a model with a belongs_to and a has_many association. The setup you describe is interesting, but not currently supported. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Jack Christensen
2006-Apr-20 23:34 UTC
Re: Re: has_many :through with has_many/has_many join models
Josh Susser wrote:
Jack Christensen wrote:
It seems that using a join model that joins with two has_many''s will
fail to generate proper SQL
class StudentSemesterRecord < ActiveRecord::Base
belongs_to :semester
has_many :discipline_records, :through => :semester
end
class Semester < ActiveRecord::Base
has_many :student_semester_records
has_many :discipline_records
end
class DisciplineRecord < ActiveRecord::Base
belongs_to :semester
end
You can do a has_many :through two ways. One is using a join model with
two belongs_to associations. The other is to go through a model with a
belongs_to and a has_many association. The setup you describe is
interesting, but not currently supported.
My fault. I was going at it the wrong way. The models are
rather extensive and I stripped a little too much out for the original
post. There is also a Person model. StudentSemesterRecord and
DisciplineRecord both belong_to Person. So it actually needn''t go
through Semester, it can search with two conditions directly on
DisciplineRecord:
has_many :discipline_records,
:finder_sql => ''SELECT * FROM discipline_records
WHERE
semester_id=#{semester_id} AND person_id=#{student_id}''
The problem is using finder_sql stops me from being able to use :order
or :conditions on find or count.
It appears I really want to use with_scope to filter to the semester
level and then use the regular has_many associations from Person.
Thanks!
Jack
--
Josh Susser
http://blog.hasmanythrough.com
--
Jack Christensen
jackc-/SOt/BrQZzMOf2zXYvRtkodd74u8MsAO@public.gmane.org
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Josh Susser
2006-Apr-21 01:16 UTC
[Rails] Re: Re: has_many :through with has_many/has_many join models
Jack Christensen wrote:> _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railslistserv seems to have eaten your reply -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.