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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs