Per suggestion, there is sufficient detail to warrant separating this as an answer.
I have tracked the problem down to a model that defines method_missing
as part of its implementation. I have found that, if there is a method_missing
definition in the model, then it is called instead of any accessors. This causes any model set-up to fail.
(in my specific case, method_missing was defined and this is what caused the stack overflow that I mentioned originally).
I can reproduce the problem succinctly by defining a new rails app in Rails 3.2.14 and then creating a new model:
class Item < ActiveRecord::Base
attr_accessible :name, :content
store :content
def method_missing(id, *args)
puts "method missing: #{id}"
end
end
and the associated migration
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.text :content
t.timestamps
end
end
end
If I run a rails console I can exercise the model:
$ rails console
Loading 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 >
If I build exactly the same thing in Rails 4.0.0 I get different output:
$ rails console
Loading 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 >
You'll notice that, 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 am reading up on the changes to ActiveRecord but I haven't been able to find anything that suggests models using method_missing
which were ok prior to Rails 4 would no longer be ok in Rails 4.
Any pointers to getting the code example able to work in Rails 4 would help me solve the problems that I have with my model.
UPDATE
By manually invoking the upward method_missing
chain, I can get the above example to work:
def method_missing(id, *args)
super
if respond_to? id
send(id,*args)
else
puts "method missing: #{id}"
end
end
Needing to do this feels wrong to me because it isn't clear why this behaviour changed between Rails 3 and 4. I feel I am missing something else...