Hi, how can i detect changed attributes, when i''m using update_attributes in the update method of my controller? A solution in the model would be very nice. Tanks greetings Leon -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You can do it with update_attributes as that updates and saves in one go. You can do this instead @thing.attributes=(params[:thing]) if @thing.changed? @thing.save else ....something else end All the best RobL On 1 March 2010 08:26, LeonS <leonard.stellbrink-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > how can i detect changed attributes, when i''m using update_attributes > in the update method of my controller? > A solution in the model would be very nice. Tanks > > greetings > Leon > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Rob Lacey contact-+WAvBcCRUVA@public.gmane.org http://www.robl.me -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi,
I thought about your solution and now I think i''ve got a better
solution, what do you think about that?
I just overwrite the update_attributes method and after i assigned the
attributes, i can check for changes.
def update_attributes(attributes)
self.attributes = attributes
if self.changed?
#kickoff something....
end
save
end
What do you think about this solution?
On 1 Mrz., 09:49, Rob Lacey <cont...-+WAvBcCRUVA@public.gmane.org>
wrote:> You can do it with update_attributes as that updates and saves in one go.
>
> You can do this instead
>
> @thing.attributes=(params[:thing])
> if @thing.changed?
> @thing.save
> else
> ....something else
> end
>
> All the best
>
> RobL
>
> On 1 March 2010 08:26, LeonS
<leonard.stellbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
>
>
> > Hi,
>
> > how can i detect changed attributes, when i''m using
update_attributes
> > in the update method of my controller?
> > A solution in the model would be very nice. Tanks
>
> > greetings
> > Leon
>
> > --
> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > For more options, visit this group
athttp://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> Rob Lacey
> cont...-QR4v9p4Fy4eTIk+IAXs5gg@public.gmane.org://www.robl.me
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 March 2010 09:06, LeonS <leonard.stellbrink-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def update_attributes(attributes) > self.attributes = attributes > if self.changed? > #kickoff something.... > end > save > end > > What do you think about this solution? >The problem with both yours and Rob''s solution is that you''re doing your post-update checks in one place, when there''s more than one place those updates can happen. Your modification of Rob''s suggestion is better, because at least you''re checking in the model, rather than in the controller. But you might be better off doing your check regardless of where the updates have come from (either a single attribute being assigned, or a mass allocation), so put a "before_save" filter in your model: before_save :check_changed def check_changed puts (changed? ? "changed" : "unchanged") end Of course, this might *cause* problems for your specific requirement, if, for instance, there''s certain fields you want to ignore changes for. But you can always expand the "check_changed" method to do more work; check the changed_attributes hash, etc. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.