# Foreign keys

**URL:** <https://www.ruby-forum.com/t/foreign-keys/204147>\
**Category:** Rails\
**Created:** [March 6, 2011, 6:29pm UTC](https://www.ruby-forum.com/t/foreign-keys/204147 "2011-03-06T18:29:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 6, 2011, 6:29pm UTC](https://www.ruby-forum.com/t/foreign-keys/204147/1 "2011-03-06T18:29:51Z")

</div>

Hi  
I am not sure how foreign keys work here, i used belongs\_to and  
has\_many and created an extra attribute for the foreign key my  
question is how can i say this is the foreign key

Thanks in advance

---

<div class="post-metadata">

**Author:** ![mohamed\_mosaad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@mohamed\_mosaad](https://www.ruby-forum.com/u/mohamed_mosaad)\
**Post date:** [March 6, 2011, 6:39pm UTC](https://www.ruby-forum.com/t/foreign-keys/204147/2 "2011-03-06T18:39:54Z")

</div>

On Sun, Mar 6, 2011 at 4:17 PM, mohamed mosaad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am not sure how foreign keys work here, i used belongs\_to and  
> has\_many and created an extra attribute for the foreign key my  
> question is how can i say this is the foreign key

If the schema follows AR naming conventions the FK is inferred.

For example, if post has many comments

```
has_many :comments, :dependent => :destroy

```

and a comment belongs to a post

```
belongs_to :post

```

and the FK in the comments table is called post\_id, you’re done. AR  
will figure it out.

If the column is called something else there’s an option to specify  
which is the FK, but if you’re defining a new schema it is better (and  
expected) to just follow the conventions.

---

<div class="post-metadata">

**Author:** ![mohamed\_mosaad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@mohamed\_mosaad](https://www.ruby-forum.com/u/mohamed_mosaad)\
**Post date:** [March 6, 2011, 6:41pm UTC](https://www.ruby-forum.com/t/foreign-keys/204147/3 "2011-03-06T18:41:09Z")

</div>

On 6 March 2011 15:17, mohamed mosaad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I am not sure how foreign keys work here, i used belongs\_to and  
> has\_many and created an extra attribute for the foreign key my  
> question is how can i say this is the foreign key

Assuming that you follow the Rails conventions then you do not need  
to, so if you have Foo belongs to bar and Bar has many foos, then  
Rails will expect the foos table to have a column bar\_id as the  
foreign key.

However you may like to look at the foreigner gem which allows foreign  
key constraints to be applied to the database to prevent invalid  
foreign keys being saved. If you are just starting then it is not  
necessary to do this for the moment. I recommend it for production  
however.

I suggest running through some guides and tutorials which will help  
you to understand how all this works. [railstutorial.org](http://railstutorial.org) is a good  
free online tutorial and you should look at the Rails Guides also.

Colin
