Ruby on Rails: building a model with an attribute not in the database?

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-04-18T22:13:21Z Indexed on 2010/04/18 22:53 UTC
Read the original article Hit count: 249

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby