felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-15 09:35 UTC
Reading Model data before update
Hi all,
here is the challenge:
I have this validation method, that basically prevents an object which
billed = true to be updated:
def before_update
if read_attribute(:billed)
errors.add_to_base(''This service has already been billed
for'')
end
!read_attribute(:billed)
end
The above code does not work in case I do something like:
object = Model.find(:first)
object.billed = true
object.save
because in my before_update filter I am reading the billed column,
that now has ''true'', so the validation returns false and the
record is
not saved.
The purpose of this validation is to prevent a record that has been
marked billed to be changed. But how then am I going to update billed
to true?
I''ve tried using
self.service.update_attribute(''billed'', true), doest
work. update_attribute_with_validation_skipping returns unknown method
error.
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On Dec 15, 2007 1:35 AM, felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > here is the challenge: > > I have this validation method, that basically prevents an object which > billed = true to be updated: > > def before_update > if read_attribute(:billed) > errors.add_to_base(''This service has already been billed for'') > end > !read_attribute(:billed) > end > > The above code does not work in case I do something like: > > object = Model.find(:first) > object.billed = true > object.save > > because in my before_update filter I am reading the billed column, > that now has ''true'', so the validation returns false and the record is > not saved. > > The purpose of this validation is to prevent a record that has been > marked billed to be changed. But how then am I going to update billed > to true? > > I''ve tried using self.service.update_attribute(''billed'', true), doest > work. update_attribute_with_validation_skipping returns unknown method > error. > > Thanks! > > >Just find the old one: old_model = Model.find id and then verify some stuff on the old model. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-15 21:52 UTC
Re: Reading Model data before update
I knew it was easy lol Thanks a lot. On Dec 15, 3:31 am, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 15, 2007 1:35 AM, felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi all, > > > here is the challenge: > > > I have this validation method, that basically prevents an object which > > billed = true to be updated: > > > def before_update > > if read_attribute(:billed) > > errors.add_to_base(''This service has already been billed for'') > > end > > !read_attribute(:billed) > > end > > > The above code does not work in case I do something like: > > > object = Model.find(:first) > > object.billed = true > > object.save > > > because in my before_update filter I am reading the billed column, > > that now has ''true'', so the validation returns false and the record is > > not saved. > > > The purpose of this validation is to prevent a record that has been > > marked billed to be changed. But how then am I going to update billed > > to true? > > > I''ve tried using self.service.update_attribute(''billed'', true), doest > > work. update_attribute_with_validation_skipping returns unknown method > > error. > > > Thanks! > > Just find the old one: > > old_model = Model.find id > > and then verify some stuff on the old model. > > Pat--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---