# Noob issue with virtual attributes

**URL:** <https://www.ruby-forum.com/t/noob-issue-with-virtual-attributes/184825>\
**Category:** Rails\
**Created:** [March 9, 2010, 7:31am UTC](https://www.ruby-forum.com/t/noob-issue-with-virtual-attributes/184825 "2010-03-09T07:31:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 9, 2010, 7:31am UTC](https://www.ruby-forum.com/t/noob-issue-with-virtual-attributes/184825/1 "2010-03-09T07:31:39Z")

</div>

Hello,  
I have 2 models, city and tip. A city an have only one tip. So what i am  
trying to do is to add my tip properties to the city form and create or  
update the city with his child the tip.  
but what i’ve done so far isn’t working… I have this error: undefined  
method `build’ for nil:NilClass

Model City:  
class City \< ActiveRecord::Base  
attr\_accessible :name, :continent, :tip\_attribute

has\_one :shop  
belongs\_to :city\_preferences  
belongs\_to :tip

validates\_presence\_of :name,:continent  
def tip\_attribute  
tip  
end

def tip\_attribute=(attribute)  
tip.build(attribute)  
end

end

City form view:  
\<% form\_for @city do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<%= f.label :continent %\>  
 \<%= f.text\_field :continent %\>

\<%= f.label :Tip %\>

 \<%fields\_for "city[tip\_attribute]", @city.tip do |t| %\> 

Min \<%= t.text\_field :min %\>

Max \<%= t.text\_field :max %\> \<%end%\>

\<%= f.submit "Submit" %\>

\<% end %\>

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 9, 2010, 9:13am UTC](https://www.ruby-forum.com/t/noob-issue-with-virtual-attributes/184825/2 "2010-03-09T09:13:10Z")

</div>

On Mar 9, 6:31 am, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I have 2 models, city and tip. A city an have only one tip. So what i am  
> trying to do is to add my tip properties to the city form and create or  
> update the city with his child the tip.  
> but what i’ve done so far isn’t working… I have this error: undefined  
> method `build’ for nil:NilClass

> def tip\_attribute=(attribute)  
> tip.build(attribute)  
> end

If you have a has\_many you can do tips.build(…) but with belongs\_to  
and has\_one you need to call build\_tip instead

Fred

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 9, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/noob-issue-with-virtual-attributes/184825/3 "2010-03-09T17:32:46Z")

</div>

Frederick C. wrote:

> If you have a has\_many you can do tips.build(…) but with belongs\_to  
> and has\_one you need to call build\_tip instead
> 
> Fred

Thanks Fred, it works better now. But an have another issue. Now the  
data are stored in the database but the column city\_it in the table tips  
is nil.  
Do i have the set it the setter like this?

def tip\_attribute=(attribute)  
attribute[:city\_id] = self.id  
build\_tip(attribute)  
end

Tip model:  
class Tip \< ActiveRecord::Base  
attr\_accessible :min,:max,:city\_id

has\_one :city

validates\_numericality\_of :min, :only\_integer =\> true  
validates\_numericality\_of :max, :only\_integer =\> true  
end

It does work if I do this, but is it the correct way to do it? should it  
be filled automaticly by active record?

Greg
