Forum: Ruby on Rails Set the foreign key constraint column name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Duane (Guest)
on 2006-01-03 22:44
I am applying Rails to an existing schema and not sure how much the
existing developers will let me go in and rename things.  So I need to
discover the limits of what I can change with Rails.

I've found set_table_name and set_primary_key, which have been very
useful.

If I have a "foo has_one :bar" relationship, but then in the bar table
my column is named "fooid" or some other thing that is not "foo_id", is
there a similar helper command I can use to change it?  Everything's
working swimmingly for me, except when I get to this bit and it errors
out telling me "cannot find bar.foo_id" and I can't figure out a way to
point to bar.fooid and say "There it is."

I tried googling around for "set foreign key constraint column name" but
can't seem to track anything down.

Thanks!
Kevin O. (Guest)
on 2006-01-03 22:46
Duane wrote:
> I am applying Rails to an existing schema and not sure how much the
> existing developers will let me go in and rename things.  So I need to
> discover the limits of what I can change with Rails.
>
> I've found set_table_name and set_primary_key, which have been very
> useful.
>
> If I have a "foo has_one :bar" relationship, but then in the bar table
> my column is named "fooid" or some other thing that is not "foo_id", is
> there a similar helper command I can use to change it?  Everything's
> working swimmingly for me, except when I get to this bit and it errors
> out telling me "cannot find bar.foo_id" and I can't figure out a way to
> point to bar.fooid and say "There it is."
>
> I tried googling around for "set foreign key constraint column name" but
> can't seem to track anything down.
>
> Thanks!

Look for the docs for has_one
There is a :foreign_key option there

http://api.rubyonrails.com/classes/ActiveRecord/As...
Jan P. (Guest)
on 2006-01-03 22:50
(Received via mailing list)
Hi, Duane,

the relationships (for example has_many) take options. One of these
options is foreign_key. See
http://api.rubyonrails.com/classes/ActiveRecord/As...
for example.

Regards
Jan
Duane (Guest)
on 2006-01-03 23:38
Jan P. wrote:
> Hi, Duane,
>
> the relationships (for example has_many) take options. One of these
> options is foreign_key. See
> http://api.rubyonrails.com/classes/ActiveRecord/As...
> for example.
>
> Regards
> Jan

Thanks everybody, sorry for the RTFM.  I was going through "Agile Web
Development with Rails" and glanced right over it because that option
was discussed as part of belongs_to, not has_to.  That's what I get for
speedskimming.

I knew it had to be an easy answer :).

d
Pat M. (Guest)
on 2006-01-04 04:04
(Received via mailing list)
foo has_one :bar, :foreign_key => "fooid"
This topic is locked and can not be replied to.