# Foreign Keys Put in DB Automatically, no?

**URL:** <https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291>\
**Category:** Rails\
**Created:** [September 20, 2006, 12:45am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291 "2006-09-20T00:45:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 12:45am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/1 "2006-09-20T00:45:21Z")

</div>

I have the following relationships:

class User \< ActiveRecord::Base  
has\_many :pictures  
end

class Picture \< ActiveRecord::Base  
belongs\_to :user  
end

And now I hace the following code:

```
@user = User.find params[:id]
@picture = @user.pictures.new params[:picture]

```

But for user\_id to be saved in my pictures db I have to add this line of  
code:

```
@picture.user_id = @user.id # should not need this... why?

```

That makes no sense. I though all this time the foreign key was  
automatically stored. Am I going crazy?

Thanks for your input :-).

Your Friend,

John

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 1:15am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/2 "2006-09-20T01:15:24Z")

</div>

On 9/19/06, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That makes no sense. I though all this time the foreign key was  
> automatically stored. Am I going crazy?

@picture = @user.pictures.build(params[:picture])

.new does look awfully nice there, however.

jeremy

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 1:31am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/3 "2006-09-20T01:31:27Z")

</div>

On 9/19/06, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And now I hace the following code:
> 
> ```
> @user = User.find params[:id]
> @picture = @user.pictures.new params[:picture]
> 
> ```

(Debatable if you need an instance var here, but let’s assume you do)  
@picture = Picture.new(params[:picture])  
@user \<\< @picture

And/or what Jeremy posted. (I find that over-golfing the solution a  
bit hard to read, but that’s me.)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 1:31am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/4 "2006-09-20T01:31:27Z")

</div>

Are you sure… it works and everything so thanks but according to  
docs build is protected method and we should use new instead… is  
this new?

On 9/19/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> jeremy
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 2:07am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/5 "2006-09-20T02:07:30Z")

</div>

You are right!

But it does say here that build is protected:  
[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods/JoinDependency.html#M000539](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods/JoinDependency.html#M000539)

On 9/19/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> protected and new should be used instead?
> 
> jeremy
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 20, 2006, 1:53am UTC](https://www.ruby-forum.com/t/foreign-keys-put-in-db-automatically-no/69291/6 "2006-09-20T01:53:29Z")

</div>

On 9/19/06, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are you sure… it works and everything so thanks but according to  
> docs build is protected method and we should use new instead… is  
> this new?

See the has\_many docs. Where are you seeing the docs saying build is  
protected and new should be used instead?

jeremy
