Hey guys :) I have some models that include computed fields, i.e. fields that aren't data as such on a table, but are computed using other fields in the table, like author.printed_name which is computed (on the sql server) by concatenating first names, last name, and name suffix. If a try to save a model that has a property like this, the sql server throws an error that it can't modify a computed column. Which is understandable :) I just don't know how to prevent that particular property/column from being saved when doing author.save(). Anyone? Thanks, Daniel :)
on 2007-03-09 15:39
on 2007-03-09 16:29
Hi Daniel, Daniel Smedegaard Buus wrote: > I have some models that include computed fields, i.e. fields that > aren't data as such on a table, but are computed using other fields in > the table, like author.printed_name which is computed (on the sql > server) by concatenating first names, last name, and name suffix. Without having any intention of getting into a philosophical debate re: 'best way' ... In Rails, you do this sort of thing in the Model rather than in the database. My recommendation would be to turn the field in the database into a 'normal' string, move the computation into a method in the model, and use a before filter to invoke it. hth, Bill
on 2007-03-09 17:38
And, if possible, remove the field from the DB entirely, and just use Rails to simulate a field that doesn't really exist... unless there's a specific reason for having your 'faux' field in the DB as opposed to in the environment accessing the DB.
on 2007-03-09 17:40
Daniel Smedegaard Buus wrote: > being saved when doing author.save(). Have a look at the attr_readonly patch at http://dev.rubyonrails.org/ticket/6896 -- We develop, watch us RoR, in numbers too big to ignore.
on 2007-03-12 11:47
Hey guys! I had a look at the diff before doing anything else, and I didn't like that approach too much, really. So I asked around today if any other application was actually going to use this computed column, and it turned out that no, only the Rails app would be using it. So, I chose to discard it, and implement the concatenation in Rails. Thanks for everyone's tips! Daniel :)