Evan
2011-Jan-08 04:06 UTC
Manipulating attributes between database retrieval and return from finder
Is there a way to manipulate the attributes of an ActiveRecord-based model after it is retrieved from the database but before it is returned from the finder? I have tried defining an after_find callback method but was unable to access attributes that come from the database from this method. I tried accessing them using self.an_attribute and read_attribute but both raised an attribute exception. I am trying to execute an upgrade-in-place strategy. When I deploy the latest version of my app one of the attributes on every existing record of a particular table will no longer be usable until converted to a new format. Rather than take the app down for 12 hours to upgrade the existing data I thought it could be upgraded on-access. Evan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jan-08 18:48 UTC
Re: Manipulating attributes between database retrieval and return from finder
On Jan 8, 4:06 am, Evan <evancha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a way to manipulate the attributes of an ActiveRecord-based > model after it is retrieved from the database but before it is > returned from the finder? > > I have tried defining an after_find callback method but was unable to > access attributes that come from the database from this method. I > tried accessing them using self.an_attribute and read_attribute but > both raised an attribute exception.Can you show exactly what you did and exactly what happened ? I can''t imagine why this wouldn''t work. Fred> > I am trying to execute an upgrade-in-place strategy. When I deploy the > latest version of my app one of the attributes on every existing > record of a particular table will no longer be usable until converted > to a new format. Rather than take the app down for 12 hours to upgrade > the existing data I thought it could be upgraded on-access. > > Evan-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Jan-08 20:46 UTC
Re: Manipulating attributes between database retrieval and return from finder
On 8 January 2011 04:06, Evan <evanchaney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a way to manipulate the attributes of an ActiveRecord-based > model after it is retrieved from the database but before it is > returned from the finder? > > I have tried defining an after_find callback method but was unable to > access attributes that come from the database from this method. I > tried accessing them using self.an_attribute and read_attribute but > both raised an attribute exception.I think that if you implemented after_find and did find all that it would have to run through the whole set, so would presumably take 12 hours. Maybe you do not do a find all, or even a find many, in which case it should be able to work.> > I am trying to execute an upgrade-in-place strategy. When I deploy the > latest version of my app one of the attributes on every existing > record of a particular table will no longer be usable until converted > to a new format. Rather than take the app down for 12 hours to upgrade > the existing data I thought it could be upgraded on-access.An alternative might be to do the test/upgrade in the access method(s) for whatever attribute(s) are affected. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.