# Verifying best practice

**URL:** <https://www.ruby-forum.com/t/verifying-best-practice/53962>\
**Category:** Rails\
**Created:** [February 18, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962 "2006-02-18T17:55:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 18, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962/1 "2006-02-18T17:55:56Z")

</div>

i have a Project model, which belongs\_to :user

when i add a project, the users\_id is based on the session[:users\_id].

to get the users\_id into the projects table, i’m doing this:

@project = Project.new(params[:project])  
@project.users\_id = session[:users\_id]

this is working, i’m just verifying this is the best way to do it.

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 18, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962/2 "2006-02-18T18:53:27Z")

</div>

On Feb 18, 2006, at 9:53 AM, matthew collins wrote:

> 

Yup, that looks good, Matthew.

You may want to change “users\_id” to “user\_id”, however, since the  
latter is the Rails naming convention and will make your belongs\_to  
and has\_many associations work without specifiying a foreign\_key.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 18, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962/3 "2006-02-18T21:10:00Z")

</div>

On Feb 18, 2006, at 8:53 AM, matthew collins wrote:

> to get the users\_id into the projects table, i’m doing this:
> 
> @project = Project.new(params[:project])  
> @project.users\_id = session[:users\_id]
> 
> this is working, i’m just verifying this is the best way to do it.

It’s more Rails idiomatic to let the associations handle the assignment,  
and to use singular foreign key naming:

If you have a @user object around:

@project = Project.new(params[:project])  
@project.user = @user

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 19, 2006, 3:01am UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962/4 "2006-02-19T03:01:23Z")

</div>

Sorry, the above is assuming a has\_one relationship between user and  
project. If it is has\_many, you want:

@project = @user.projects.build( params[:project] )

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 19, 2006, 3:01am UTC](https://www.ruby-forum.com/t/verifying-best-practice/53962/5 "2006-02-19T03:01:23Z")

</div>

Even better (again assuming the presence of a loaded user object):

@project = @user.project.build( params[:project] )  
@project.save
