Forum: Ruby on Rails has_many :through with has_many/has_many join models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89441a6c74647d292f5cc951eae24cfa?d=identicon&s=25 Jack Christensen (Guest)
on 2006-04-21 00:57
(Received via mailing list)
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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-21 01:15
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.

--
Josh Susser
http://blog.hasmanythrough.com
89441a6c74647d292f5cc951eae24cfa?d=identicon&s=25 Jack Christensen (Guest)
on 2006-04-21 01:36
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-21 03:16
Jack Christensen wrote:
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

listserv seems to have eaten your reply

--
Josh Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.