has_one association against a non primary key

112 views
Skip to first unread message

Mikel Lindsaar

unread,
Feb 6, 2008, 6:41:33 PM2/6/08
to Ruby on Rails: Talk
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

Greg Donald

unread,
Feb 12, 2008, 5:20:07 PM2/12/08
to rubyonra...@googlegroups.com

Mikel Lindsaar

unread,
Feb 12, 2008, 7:07:19 PM2/12/08
to rubyonra...@googlegroups.com
On Feb 13, 2008 9:20 AM, Greg Donald <gdo...@gmail.com> wrote:
> On Feb 6, 2008 5:41 PM, Mikel Lindsaar <raas...@gmail.com> 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

Reply all
Reply to author
Forward
0 new messages