# Model Attributes

**URL:** <https://www.ruby-forum.com/t/model-attributes/174219>\
**Category:** Rails\
**Created:** [August 22, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/model-attributes/174219 "2009-08-22T19:23:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@Glen](https://www.ruby-forum.com/u/Glen)\
**Post date:** [August 22, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/model-attributes/174219/1 "2009-08-22T19:23:17Z")

</div>

I’m having trouble with a model:

class Job \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :city  
belongs\_to :region  
belongs\_to :country  
belongs\_to :department  
belongs\_to :user

has\_many :comments

accepts\_nested\_attributes\_for :city, :allow\_destroy =\> :false  
accepts\_nested\_attributes\_for :region, :allow\_destroy =\> :false  
accepts\_nested\_attributes\_for :country, :allow\_destroy =\> :false  
accepts\_nested\_attributes\_for :company, :allow\_destroy =\> :false  
accepts\_nested\_attributes\_for :department, :allow\_destroy =\> :false  
end

I’m trying to create a form with nested attributes but whenever I call  
@job.city.build it complains that @job.city is nil.

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.build

Here is the associated controller code:

def new  
@job = Job.new

```
@job.city.build
@job.region.build
@job.counrty.build
@job.department.build
@job.company.build

```

end

It would appear that I am wrong in my assumption that the relation in  
the model allows me to treat city as an attribute. How would I go  
about making this work?

---

<div class="post-metadata">

**Author:** ![Glen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@Glen](https://www.ruby-forum.com/u/Glen)\
**Post date:** [August 22, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/model-attributes/174219/2 "2009-08-22T23:22:07Z")

</div>

On Aug 22, 6:22 pm, Glen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having trouble with a model:  
> def new  
> @job = Job.new
> 
> ```
> @job.city.build
> @job.region.build
> @job.counrty.build
> @job.department.build
> @job.company.build
> 
> ```

The magically created methods are @job.build\_city etc.

Fred

---

<div class="post-metadata">

**Author:** ![Glen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@Glen](https://www.ruby-forum.com/u/Glen)\
**Post date:** [August 23, 2009, 12:13am UTC](https://www.ruby-forum.com/t/model-attributes/174219/3 "2009-08-23T00:13:02Z")

</div>

On Aug 22, 3:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > @job.company.build
> > 
> > ```
> 
> The magically created methods are @job.build\_city etc.

Thank you much, that did the trick.

---

<div class="post-metadata">

**Author:** ![Glen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@Glen](https://www.ruby-forum.com/u/Glen)\
**Post date:** [August 23, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/model-attributes/174219/4 "2009-08-23T13:06:17Z")

</div>

Fred’s got the answer on this one, but holy database normalization  
Batman! A ‘country’ string field is enough for most people…

–Matt J.
