Ritz Coder
2006-Jul-14 18:37 UTC
[Rails] creating a computed column with activerecord for to_xml
Hi. I have a question about activerecord, computed columns and to_xml. I have a simple model called person that has a first_name and a last_name field (and thus a first_name and last_name field in the Person table). When I :find a model and call to_xml, I would like the result to not only send back first name and last name as xml but also add an additional attribute called full_name that is "last_name, first_name" I would like to put this logic in my model. I have made a def that returns this in the model - so you can call theperson.fullname in the console and it will return last_name, first_name. However, I would like to_xml to do this also. This is basically the concept of a computed column. I tried adding to the column hash in Person and many other ideas but nothing works. Can anyone help with this? Thanks very much. Ritz -- Posted via http://www.ruby-forum.com/.
Eric Anderson
2006-Jul-15 01:14 UTC
[Rails] Re: creating a computed column with activerecord for to_xml
Ritz Coder wrote:> Hi. I have a question about activerecord, computed columns and to_xml.[...snip details...] You could simply add the "computed" column to your database and then use a "before_save" callback to compute this value. Only downside is that unsaved records would have NULL for full_name when doing to_xml. You could even get around this through additional code. Eric
Reasonably Related Threads
- is it possible to make to_xml use underscore instead of dash?
- howto rebuild a object from a received xml hash structure ?
- xx-0.1.0 : xhtml and xml make it twice as dirty
- Rendering all the elements when calling Array#to_xml
- Confused about to_xml() in ActiveRecord::Base subclass