# How to add a new attribute to a model

**URL:** <https://www.ruby-forum.com/t/how-to-add-a-new-attribute-to-a-model/71949>\
**Category:** Rails\
**Created:** [October 12, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-add-a-new-attribute-to-a-model/71949 "2006-10-12T19:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 12, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-add-a-new-attribute-to-a-model/71949/1 "2006-10-12T19:10:00Z")

</div>

I have a model

class PriceSet \< ActiveRecord::Base  
def price\_type\_description  
case read\_attribute(“price\_type”)  
when “P”  
self[:price\_type\_description] = “Promotion”  
when “F”  
self[:price\_type\_description] = “Front L.”  
else  
self[:price\_type\_description] = “??”  
end  
end  
end

using irb

> > ps=PriceSet.find(7)  
> > =\> #\<PriceSet:0x44f0674 @attributes={“store\_set\_id”=\>1, “vendor\_id”=\>10,  
> > “price\_  
> > to\_start\_on”=\>#\<Date: 4907775/2,0,2299161\>, “approved\_by\_user\_id”=\>nil,  
> > “id”=\>7,  
> > “price\_status”=\>“C”, “approved\_at”=\>nil, “price\_type”=\>“F”,  
> > “price\_to\_end\_on”=\>  
> > #\<Date: 4910755/2,0,2299161\>}\>
> 
> > ps.price\_type  
> > =\> “F”
> 
> > ps.price\_type\_description  
> > =\> “Front L.”

Why does price\_type\_description not appear as one of the attributes?

I want price\_type\_description to be one of the attributes when I do a  
find  
on this class, can I do this?

So I can have both price\_type and price\_type\_description in the xml  
output

class PriceSetController \< ApplicationController  
def list  
@price\_set = PriceSet.find(:all,  
:conditions =\> [“vendor\_id = :vendor\_id”,  
params],  
:order =\> “price\_to\_start\_on, price\_type,  
price\_status”)  
render :xml =\> @price\_set.to\_xml  
end  
end

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 12, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/how-to-add-a-new-attribute-to-a-model/71949/2 "2006-10-12T20:07:05Z")

</div>

Andrew C. wrote:

> I have a model
> 
> class PriceSet \< ActiveRecord::Base  
> def price\_type\_description  
> case read\_attribute(“price\_type”)  
> when “P”  
> self[:price\_type\_description] = “Promotion”  
> when “F”  
> self[:price\_type\_description] = “Front L.”  
> else  
> self[:price\_type\_description] = “??”  
> end  
> end  
> end
> 
> So I can have both price\_type and price\_type\_description in the xml  
> output

Only database column are considered “attributes” by ActiveRecord. The  
vanilla to\_xml method wont do everything for you all the time. Perhaps  
you should override to to\_xml method on your model so that you get the  
output exactly how you want it.

Check out the bottom of  
[http://caboo.se/doc/classes/ActiveRecord/XmlSerialization.html#M005970](http://caboo.se/doc/classes/ActiveRecord/XmlSerialization.html#M005970)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/how-to-add-a-new-attribute-to-a-model/71949/3 "2006-10-18T10:38:56Z")

</div>

Try

attr\_accessor :price\_type\_description

Jason
