Has_one association against a non primary key

Hey list,

Any easy way to do a has_one, belongs_to based off a key that is NOT
the ID field?

I am working with a legacy table that has a grouping column in the
table, I need to have a has_one association off this grouping column.

Otherwise, I guess I’ll just have to make a method, which isn’t a big
deal.

Regards

Mikel

On Feb 6, 2008 5:41 PM, Mikel L. [email protected] wrote:

Any easy way to do a has_one, belongs_to based off a key that is NOT
the ID field?

I am working with a legacy table that has a grouping column in the
table, I need to have a has_one association off this grouping column.

Otherwise, I guess I’ll just have to make a method, which isn’t a big deal.

Both has_one and belongs_to accept a :foreign_key parameter.

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001104
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001105


Greg D.
http://destiney.com/

On Feb 13, 2008 9:20 AM, Greg D. [email protected] wrote:

On Feb 6, 2008 5:41 PM, Mikel L. [email protected] wrote:

Any easy way to do a has_one, belongs_to based off a key that is NOT
the ID field?

I am working with a legacy table that has a grouping column in the
table, I need to have a has_one association off this grouping column.

Otherwise, I guess I’ll just have to make a method, which isn’t a big deal.

Both has_one and belongs_to accept a :foreign_key parameter.

No, I know that, but the foreign_key param expects the source record’s
ID value to be in the ID column.

What I am talking about is a self referrential table where a person
might have several records in the table, but these records are grouped
by a grouping value, like an entity id column.

So each record has it’s own unique ID, but many records could share
the grouping ID, no one record is the “parent”, as each record in the
group belongs to each other.

But maybe I’ll check out the self-refferential stuff again, I might
have missed something obvious.

Mikel