I''d like to join two tables without using the primary key on either. Let''s say I have the following tables: /items id name item_other_key /item_links id link_id item_other_key created_at I''d like to say item_link belongs_to :item, :foreign_key => ''item_other_key'' But Rails decides to join on (item_links.item_other_key = items.id) instead of (item_links.item_other_key = items.item_other_key). The data is coming from an external source, which is why I need to use external keys. My workaround right now is to just do the join manually with a find_by_sql. Am I missing a clean Railsy way to accomplish this? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Drew wrote:> I''d like to join two tables without using the primary key on either. > Let''s say I have the following tables: > > /items > id > name > item_other_key > > /item_links > id > link_id > item_other_key > created_at > > I''d like to say > item_link belongs_to :item, :foreign_key => ''item_other_key'' > > But Rails decides to join on (item_links.item_other_key = items.id) > instead of (item_links.item_other_key = items.item_other_key). > > The data is coming from an external source, which is why I need to use > external keys. My workaround right now is to just do the join manually > with a find_by_sql. Am I missing a clean Railsy way to accomplish this?You need to set a :foreign_key on the has_one/has_many side of the association. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---