Forum: Ruby on Rails Foreign Keys in ActiveRecord

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.
5392a307d3b5a4173728694822211655?d=identicon&s=25 Felipe Teixeira (Guest)
on 2012-07-08 14:03
(Received via mailing list)
Hello there,

I'm a beginner in Rails, so forgive me if this question is too dumb ;p
Does Rails provide any helper/linecode to create a foreign key in the
database (via migration)?

When I use, for example, *belogs_to :class, :foreign_key => id_class*,
I'm
just setting the name
of the foreign key.

In the same way, when I use *t.reference :class *in the migration*, *I'm
just creating an column *class_id *
with the appropriate class in the database... but I don't properly
create
the foreign key in database-level,
right?

So, is there a nice way to do that in Rails? I already saw some
solutions
in the web, but I couldn't find
any convention. Which would be the best way to do that?

Thanks in advance (:

P.S: sorry if the English is not good ;p
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-07-08 14:15
(Received via mailing list)
On Jul 6, 9:30pm, Felipe Teixeira <felipee...@gmail.com> wrote:
> Hello there,
>
> I'm a beginner in Rails, so forgive me if this question is too dumb ;p
> Does Rails provide any helper/linecode to create a foreign key in the
> database (via migration)?

No - the official position has never been in favour of database level
constraints.
The foreigner gems adds migration helpers for adding foreign keys

Fred
This topic is locked and can not be replied to.