Customizing Rails XML rendering to include extra properties
Posted
by Isaac Cambron
on Stack Overflow
See other posts from Stack Overflow
or by Isaac Cambron
Published on 2010-06-11T00:05:17Z
Indexed on
2010/06/11
0:12 UTC
Read the original article
Hit count: 596
ruby-on-rails
|xml-serialization
Let's say I have a model like this:
create_table :ninjas do |t|
t.string name
end
And the Ninja class with an extra property:
class Ninja < ActiveRecord::Base
def honorific
"#{name}san"
end
end
And in my controller I just want to render it to XML:
def show
render :xml => Ninja.find(params[:id])
end
The honorific part isn't rendered. That makes sense, since it's just a method, but is there a way of tricking it?
I'm totally up for answers to the effect of, "You're doing this totally wrong." I'll just add that I really do want to calculate the honorific on the fly, and not, like, store it in the database or something.
© Stack Overflow or respective owner