Wes Gamble
2006-Jun-27 17:33 UTC
[Rails] belongs_to <parent name>, :foreign_key modifier not working
All, I have two ActiveRecord objects. TargetList has_many Targets Targets belongs_to TargetList I''ve specified belongs_to :target_list, :foreign_key => ''DataSetID'' Queries generated by various methods in target_list do not seem to see the foreign_key name and keep trying to query my Target table using "target_list_id" (the default foreign key). Anyone seen this before? Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jun-27 18:53 UTC
[Rails] Re: belongs_to <parent name>, :foreign_key modifier not work
Wes Gamble wrote:> All, > > I have two ActiveRecord objects. > > TargetList has_many Targets > Targets belongs_to TargetList > > I''ve specified > > belongs_to :target_list, > :foreign_key => ''DataSetID'' > > Queries generated by various methods in target_list do not seem to see > the > foreign_key name and keep trying to query my Target table using > "target_list_id" (the default foreign key). > > Anyone seen this before? > > WesTo be more specific, in target_list.rb, I''m doing targets.size to get the count of my child object, targets. The query that''s generated correctly goes against the right table, but the foreign_key specified is default. I''m trying to puzzle through the ActiveRecord code on my own but it''s pretty brutal. Here''s some more info. I''ve discovered that by the time I get into the has_many_association.rb count_records method, already the counter_sql is incorrect. In fact when the has_many association is initialized, this SQL is incorrect. I think that there may be an issue with the fact that both of my tables (parent AND child) do a set_primary_key - I think that maybe ActiveRecord can''t handle it. And sadly, I don''t have any more time to figure out how this all works. I''ll write a custom finder to do the <child_table>.size query. Sigh. Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jun-27 19:04 UTC
[Rails] Re: belongs_to <parent name>, :foreign_key modifier not work
Oy vey!!! OK, if it helps someone later, you have to specify a non-standard foreign_key column between a parent and it''s child on BOTH sides of the association. So, in the child, do: belongs_to :parent_object_name, :foreign_key => ''X'' and in the parent, do: has_many :child_object_name, :foreign_key => ''X'' Wes -- Posted via http://www.ruby-forum.com/.
Olie D.
2008-Jan-15 19:53 UTC
Re: belongs_to <parent name>, :foreign_key modifier not work
Wes Gamble wrote:> Oy vey!!! > > OK, if it helps someone later, you have to specify a non-standard > foreign_key column between a parent and it''s child on BOTH sides of the > association. > > So, in the child, do: > > belongs_to :parent_object_name, :foreign_key => ''X'' > > and in the parent, do: > > has_many :child_object_name, :foreign_key => ''X''Oy vey, indeed! I have a similar problem, with the complication that the tables in question don''t belong to me, I''m just reporting off of them. Table Products: No real key/id field. Uniquified off of string "Product" Table Versions: No real key/id field. Has foreign-key pointer to products, but field is called "program." Sure, whatever. :\ I''m having a heck of a time setting up my models'' belongs_to and has_many lines. Can someone give me the syntax? Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---