starfry
2013-Aug-29 13:29 UTC
Why is the behaviour of ActiveRecord method_missing different between rails 3.2 and 4.0 ?
I have been working on updating a rails application to 4.0 and spent some time getting to the bottom of why it wasn''t working. What I found was, if a model has a method_missing definition then it is called *instead of any accessors*. This causes any model set-up to fail. The following example demonstrates the problem: class Item < ActiveRecord::Base attr_accessible :name, :content store :content def method_missing(id, *args) puts "method missing: #{id}" endend In Rails 3.2.14: $ rails consoleLoading development environment (Rails 3.2.14)2.0.0p247 :001 > x = Item.new(name: ''foo'') => #<Item id: nil, name: "foo", content: {}, created_at: nil, updated_at: nil> 2.0.0p247 :002 > and in Rails 4.0.0: $ rails consoleLoading development environment (Rails 4.0.0)2.0.0p247 :001 > x = Item.new(name: ''foo'') method missing: name => #<Item id: nil, name: nil, content: {}, created_at: nil, updated_at: nil> 2.0.0p247 :002 > In Rails 3.2.14, the name attribute is set to foo as is the intention. In Rails 4.0.0, however, see that method_missing is called and the attribute is not set. I can work around this by manually invoking the upward method_missing chain: def method_missing(id, *args) super if respond_to? id send(id,*args) else puts "method missing: #{id}" end end I don''t understand why this happens, or if I am doing something wrong. Would someone with more knowledge of the internals be able to explain this ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/044cf660-1c65-40ca-a2cc-eb142957a2de%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Aug-30 11:47 UTC
Re: Why is the behaviour of ActiveRecord method_missing different between rails 3.2 and 4.0 ?
On Thu, Aug 29, 2013 at 2:29 PM, starfry <google-lXKJ6yukL0FBDgjK7y7TUQ@public.gmane.org> wrote:> I have been working on updating a rails application to 4.0 and spent some > time getting to the bottom of why it wasn''t working. > > What I found was, if a model has a method_missing definition then it is > called *instead of any accessors*. This causes any model set-up to fail. > >As far as I am aware, the accessors for attributes have always been generated using method_missing (the method missing hook then actually defines the accessors, so after that method missing is not used). What might possibly have changed is when this gets triggered. Regardless if you do override method_missing it''s a good idea to call to the superclass as other aspects of active record may rely on this. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CABp077WR%3Du88x0R-T%3D6tHf73mQvk2rSpKhZ1EkMY5oxcuHzrZg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.