Customizing Rails XML rendering to include extra properties
- by Isaac Cambron
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.