I am not sure I am thinking about this the correct way. I have a admin/maintenance scaffold to amend live data and I need to track the changes to data related to an order. e.g. change of address. I have a order_transaction model which I would like to record all of the data changes through the admin maintenance. I was hoping to use the AR tracking methods to help me. Rails 2.3.8 I do the normal read the order in from the database, pick up the attributes from params[:order} and then do an @order.update_attributes(params[:order]) The problem is that on success, @order.changes is nil. In the console: ruby-1.8.7-p334 :024 > @o = Order.last Order Load (0.4ms) SELECT * FROM "orders" ORDER BY orders.id DESC LIMIT 1 +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ | id | quote_id | polic... | ip_a... | acco... | paym... | last... | amount | cc_t... | cc_s... | cc_c... | dd_p... | dd_f... | dd_m... | dd_a... | dd_s... | crea... | updat... | +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ | 25 | 23 | 21 | 192.... | Dona... | monthly | | 9.99 | | | | 8 | 2011... | false | iIwv... | 4VcQ... | 2011... | 2011-... | +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ 1 row in set ruby-1.8.7-p334 :025 > @o.update_attributes({:account_name => "Mickey Mouse"}) Order Update (0.5ms) UPDATE "orders" SET "updated_at" ''2011-07-05 18:08:51'', "account_name" = ''Mickey Mouse'' WHERE "id" = 25 => true ruby-1.8.7-p334 :027 > @o.changes => {} ruby-1.8.7-p334 :028 > @o +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ | id | quote_id | polic... | ip_a... | acco... | paym... | last... | amount | cc_t... | cc_s... | cc_c... | dd_p... | dd_f... | dd_m... | dd_a... | dd_s... | crea... | updat... | +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ | 25 | 23 | 21 | 192.... | Mick... | monthly | | 9.99 | | | | 8 | 2011... | false | iIwv... | 4VcQ... | 2011... | 2011-... | +----+----------+----------+---------+---------+---------+--------- +--------+---------+---------+---------+---------+---------+--------- +---------+---------+---------+----------+ 1 row in set ruby-1.8.7-p334 :029 > Is there a way to find out what the changes are without looping through the hash key and updating @order using the AR setter methods a column at a time, save away @order.changes and then doing an @order.save. I only need to track changes from the admin screens, not from the live side of the system. The solutions for tracking AR objects seem to be all or nothing approach. O. -- 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.
Frederick Cheung
2011-Jul-05 20:31 UTC
Re: Active Record track changes with update_attributes
On Jul 5, 7:12 pm, Owain <owain.mcgu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am not sure I am thinking about this the correct way. I have a > admin/maintenance scaffold to amend live data and I need to track the > changes to data related to an order. e.g. change of address. I have > a order_transaction model which I would like to record all of the data > changes through the admin maintenance. I was hoping to use the AR > tracking methods to help me. Rails 2.3.8 > > I do the normal read the order in from the database, pick up the > attributes from params[:order} and then do an > @order.update_attributes(params[:order]) The problem is that on > success, @order.changes is nil. In the console: >order.changes gives unsaved changes, so by design this will be empty after a successful save. You could do order.attributes = params[:order], inspect the changes and then save the object. Fred -- 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.
Philip Hallstrom
2011-Jul-05 22:02 UTC
Re: Re: Active Record track changes with update_attributes
> On Jul 5, 7:12 pm, Owain <owain.mcgu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I am not sure I am thinking about this the correct way. I have a >> admin/maintenance scaffold to amend live data and I need to track the >> changes to data related to an order. e.g. change of address. I have >> a order_transaction model which I would like to record all of the data >> changes through the admin maintenance. I was hoping to use the AR >> tracking methods to help me. Rails 2.3.8 >> >> I do the normal read the order in from the database, pick up the >> attributes from params[:order} and then do an >> @order.update_attributes(params[:order]) The problem is that on >> success, @order.changes is nil. In the console: >> > order.changes gives unsaved changes, so by design this will be empty > after a successful save. > You could do order.attributes = params[:order], inspect the changes > and then save the object.This might be useful. Worked well for me in the past. Works in Rails3 if you tweak the ''changes'' bit as that is now reserved... http://pjkh.com/articles/2009/02/02/creating-an-audit-log-in-rails -- 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 could do order.attributes = params[:order], inspect the changes > and then save the object. > > Fred@fred that worked perfectly. -- 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.