How to add a new attribute to a model

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

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

Try

attr_accessor :price_type_description

Jason