# Table associations and models best practice

**URL:** <https://www.ruby-forum.com/t/table-associations-and-models-best-practice/126139>\
**Category:** Rails\
**Created:** [January 15, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/table-associations-and-models-best-practice/126139 "2008-01-15T18:53:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@ray](https://www.ruby-forum.com/u/ray)\
**Post date:** [January 15, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/table-associations-and-models-best-practice/126139/1 "2008-01-15T18:53:31Z")

</div>

I need some help understanding the proper way to save (if possible)  
using the new(). create() method. I need an example to try to  
explain.

Say, I have a table called projects with id, name, and  
status\_id as attributes along with another table called statuses with  
id, name

My Project model will contain a “belongs\_to :status” and Status model  
will contain a “has\_many :projects”.

Status table can contain (“active”, “scheduled”, “inactive”) along  
with their respective ids.

I’m trying to figure out the proper way for, say, creating a new  
project model using new() or create() with an ‘active’ id.

I’m looking to understand the difference between these and whether I  
should always prefer to use one over the other…

p = Project.new(:name =\> “blah”, :status\_id =\>  
Status.find\_by\_name(“active”).id)

p = Project.new(:name =\> “blah”, :status\_id =\>  
Status.find\_by\_name(“active”))

p = Project.new(:name =\> “blah”, :status =\>  
Status.find\_by\_name(“active”).id)

p = Project.new(:name =\> “blah”, :status =\>  
Status.find\_by\_name(“active”))

Another question, along the same lines should I be doing the  
following?

p = Project.new(:name =\> “blah”, :status\_id =\> Status.new(:name=\>  
“test”))

p.save should create the new Status right?

Any help would be greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![ray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@ray](https://www.ruby-forum.com/u/ray)\
**Post date:** [January 17, 2008, 5:32am UTC](https://www.ruby-forum.com/t/table-associations-and-models-best-practice/126139/2 "2008-01-17T05:32:57Z")

</div>

ray wrote:

> Status.find\_by\_name(“active”).id)
> 
> p = Project.new(:name =\> “blah”, :status\_id =\>  
> Status.find\_by\_name(“active”))
> 
> p = Project.new(:name =\> “blah”, :status =\>  
> Status.find\_by\_name(“active”).id)
> 
> p = Project.new(:name =\> “blah”, :status =\>  
> Status.find\_by\_name(“active”))

If you use the enumerations\_mixin plugin you can write

p = Project.new(:name =\> “blah”, :status =\> :active)

> Another question, along the same lines should I be doing the  
> following?
> 
> p = Project.new(:name =\> “blah”, :status\_id =\> Status.new(:name=\>  
> “test”))
> 
> p.save should create the new Status right?

To create the new status you’d have assign the new Status object  
to the status attribute rather than status\_id.

–  
We develop, watch us RoR, in numbers too big to ignore.
