Daniel Smedegaard Buus
2007-Mar-09 14:38 UTC
Exclude a property on a model from save operations?
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 :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Mar-09 16:29 UTC
Re: Exclude a property on a model from save operations?
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. > > 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().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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 3/9/07, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> > > 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 > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Smedegaard Buus
2007-Mar-12 10:46 UTC
Re: Exclude a property on a model from save operations?
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 :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---