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 Line" 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 Line" 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Andrew Cleland 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 Line" > else > self[:price_type_description] = "??" > end > end > end > > So I can have both price_type and price_type_description in the xml > outputOnly 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Try attr_accessor :price_type_description Jason On 10/12/06, Andrew Cleland <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Line" > 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 Line" > > 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 > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---