Ruby on Rails: building a model with an attribute not in the database?
- by randombits
I have a model that sets one of its attributes based off of a form parameter that a user submits. The model is a child resource to a parent resource Home. A hypothetical example is the following:
A class Person has an age attribute. The user submits a birthdate as an HTTP POST parameter and from that, I need to derive the age of the user. So I'd do something like this:
@home.people.build(:name => params[:name], :birthdate => params[:birthdate])
Rails would barf on that for obvious reasons, complaining it doesn't know what the attribute birthdate is. What's the proper way of going about this?
Is it possible to use the build constructor with the supplied solution so that my foreign key relations are also setup properly? If not, what's a better way to work around this problem?