belongs_to associations on non-primary keys

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?

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.