When attempting to use a "has_many :through" association with three
models using non-standard primary keys, Rails is not constructing SQL
finder query properly. For some reason it is not using correct primary
key column for the "middle" table. Here is a detailed example:
create_table :books, :primary_key => :my_book_id, :force => true do |t|
t.column :name, :string
end
create_table :chapters, :primary_key => :my_chapter_id, :force => true
do |t|
t.column :parent_book_id, :integer
t.column :name, :string
end
create_table :paragraphs, :primary_key => :paragraph_id, :force => true
do |t|
t.column :parent_chapter_id, :integer
t.column :name, :string
end
class Book < ActiveRecord::Base
set_primary_key(:my_book_id)
has_many :chapters, :foreign_key => :parent_book_id
has_many :paragraphs, :through => :chapters, :source => :paragraphs
end
class Chapter < ActiveRecord::Base
set_primary_key(:my_chapter_id)
has_many :paragraphs, :foreign_key => :parent_chapter_id
belongs_to :book, :foreign_key => :parent_book_id
end
class Paragraph < ActiveRecord::Base
set_primary_key(:my_paragraph_id)
belongs_to :chapter, :foreign_key => :parent_chapter_id
end
Now, when I run the following from the console:
Book.find(1).paragraphs
The following SQL gets sent to MySQL:
SELECT paragraphs.* FROM paragraphs INNER JOIN chapters ON
paragraphs.parent_chapter_id = chapters.my_paragraph_id WHERE
chapters.parent_book_id = 1
The ON clause is in error. Instead of saying:
ON paragraphs.parent_chapter_id = chapters.my_paragraph_id
it should be:
ON paragraphs.parent_chapter_id = chapters.my_chapter_id
Am I missing some option in one of my associations? Or am I hitting a
Rails bug?
BTW, I am running Rails 1.2.1
Thanks in advance,
Fyodor Golos
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---