# Foreign key question

**URL:** <https://www.ruby-forum.com/t/foreign-key-question/132537>\
**Category:** Rails\
**Created:** [March 21, 2008, 5:11am UTC](https://www.ruby-forum.com/t/foreign-key-question/132537 "2008-03-21T05:11:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 5:11am UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/1 "2008-03-21T05:11:42Z")

</div>

Hi-

Say I have a db with following schema:

MyTable:  
Id, name, user\_id, date

User:  
id, name, description

I know that I want to add “belongs\_to” in my “user” model but how can  
I enforce a proper constraint on the user\_id in “MyTable”? Is this  
done using a has\_one in the “MyTable” model?

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 10:03am UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/2 "2008-03-21T10:03:08Z")

</div>

Hello,

I think you’re confusing things a bit.

With the tables you describe you should put the following into your  
models:

my\_table.rb  
belongs\_to :user

user.rb  
has\_many :my\_tables  
(or has\_one :my\_table)

If you want it the other way around, you should put the foreign key  
my\_table\_id in your User model.

Regards

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 10:54am UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/3 "2008-03-21T10:54:27Z")

</div>

> how can I enforce a proper constraint on the user\_id in “MyTable”?

If you mean constraints on a database level: you will have to put those  
in manually. AFAIK Rails itself (e.g. migrations) do not provide support  
for database level constraints of anything other than datatype (and  
length). There surely is a plugin around though…

- Niels.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/4 "2008-03-21T14:06:33Z")

</div>

Cool, this helps. One more clarification…since “MyTable” was a  
really bad example…

job\_posting.rb  
belongs\_to :user

user.rb  
has\_many :job\_postings

So, say I want to link a user to a job posting, a user can have many  
job postings but I want to make sure that the user being referenced is  
a real user. Is this the correct approach?

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/5 "2008-03-21T16:51:22Z")

</div>

Yes, that should be fine, but maybe you should add:

user.rb  
has\_many :job\_postings, :dependent =\> :destroy

Now if your user record is deleted, all it’s job postings are also  
removed from the DB. Otherwise there 's a big chance you 'll get  
nil-errors in your views (eg. when you try to display \<%=  
job\_posing.user.name %\>)

Stijn

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 21, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/foreign-key-question/132537/6 "2008-03-21T17:15:11Z")

</div>

Great, thank you!

On Mar 21, 3:51 pm, Stijn P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
