hi guys i have a product model with many columns like cat1,cat2,cat3,name,desc,price,qty etc. along with that i have a virtual attribute call full_name in model attr_accessor :full_name def full_name cat1+cat2+cat3+name #something like this end when i do Product.find(:all) in the product array the product''s hash object does not have a attrubute with the name full_name. although i know if i write prod.full_name it will work i want that attribute to be in the hash as a key-value pair as i want to convert the whole array to json. what is the simplest way of doing this. i had tried this but did not work def after_find full_name end thanks jags -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-23 13:10 UTC
Re: Activerecord''s virtual attribute in find''s array
On 23 May 2008, at 13:59, Jags Rao wrote:> > def full_name > cat1+cat2+cat3+name #something like this > end > > when i do Product.find(:all) > in the product array the product''s hash object does not have a > attrubute > with the name full_name. > > although i know if i write prod.full_name it will work > i want that attribute to be in the hash as a key-value pair > as i want to convert the whole array to json. >to_json takes a :methods parameter where you can list methods to call whose result should be in the json eg products.to_json :methods => :[full_name] Fred --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---