Forum: Ruby on Rails Exclude a property on a model from save operations?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel Smedegaard B. (Guest)
on 2007-03-09 16:39
(Received via mailing list)
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 :)
Bill W. (Guest)
on 2007-03-09 17:29
(Received via mailing list)
Hi Daniel,

Daniel Smedegaard B. 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
Luke I. (Guest)
on 2007-03-09 18:38
(Received via mailing list)
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.
Mark Reginald J. (Guest)
on 2007-03-09 18:40
(Received via mailing list)
Daniel Smedegaard B. 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.
Daniel Smedegaard B. (Guest)
on 2007-03-12 12:47
(Received via mailing list)
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 :)
This topic is locked and can not be replied to.