Skip to content

Undefined method old_[attribute]= in Rails 3.2.3 #23

Closed
orangethunder opened this Issue May 11, 2012 · 13 comments

6 participants

@orangethunder

After upgrading from Rails 3.0, I get the above. I'm just really looking for confirmation that someone else has got this gem working with Rails 3.2, so I can see if it's something I must fix on my side.
Here's the stack trace:

NoMethodError: undefined method `old_account_number=' for #<BankAccount:0x00000006089f48>
from /home/daveungerer/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /home/daveungerer/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in `method_missing'
from /home/daveungerer/.rvm/gems/ruby-1.9.3-p194/gems/attribute_normalizer-1.0.1/lib/attribute_normalizer/model_inclusions.rb:49:in `rescue in block (2 levels) in normalize_attributes'
from /home/daveungerer/.rvm/gems/ruby-1.9.3-p194/gems/attribute_normalizer-1.0.1/lib/attribute_normalizer/model_inclusions.rb:45:in `block (2 levels) in normalize_attributes'

It seems that, at the point the code in model_inclusions.rb is run, the :account_number= method is not yet defined, which causes it to never define :old_account_number=

One other thing: I'm using model inheritance, so if it turns out it that the gem is Rails 3.2 compatible, I'll investigate if that is the actual cause.

@orangethunder

Was using version 1.0.1 by accident. Upgraded to 1.1.0, same problem.

@mdeering
Owner

can you try this pull request #22 #22 to see if it resolves your issue?

@orangethunder

I can now confirm that inheritance is the issue, as I was able to work around the problem by getting rid of the inheritance.

I also tried using latest master branch, which includes the pull request mentioned above, same issue. Also, it works fine in Rails 3.0 and 3.1, just not 3.2.

Just for further info, I was calling normalize_attributes from the superclass in my inheritance hierarchy.

Thanks for the help.

Regards,
Dave

@mdeering
Owner
mdeering commented Aug 9, 2013

Found workaround not enough info to reproduce.

@mdeering mdeering closed this Aug 9, 2013
@der-flo
der-flo commented Nov 14, 2013

I stumpled over this error when upgrading an app to Rails 3.2, so here's a simple example:

class Foo < ActiveRecord::Base
  normalize_attributes :bar
end
class Baz < Foo
end

Baz.new.bar = 'txt'

This raises undefined method 'txt='

My temporary workaround is:

class Foo < ActiveRecord::Base
  def self.inherited(subclass)
    normalize_attributes :bar
    super(subclass)
  end
end
class Baz < Foo
end

Do you have a better solution?

@Napolskih

Is there a solution for today?

@mdeering
Owner

try with version 1.2.0.b

@funglaub
funglaub commented Oct 1, 2014

I tried with 1.2.0.b, resulting in an Exception

NoMethodError: super: no superclass method `bar='

with the example from #23 (comment)

@funglaub
funglaub commented Oct 8, 2014

@dimko I get exactly the same Exception with normalizr.

NoMethodError: super: no superclass method `bar='

Am I doing it wrong?

@dimko
dimko commented Oct 13, 2014

@funglaub can't reproduce.

$ cat Gemfile app/models/foo.rb app/models/baz.rb 
source 'https://rubygems.org'

gem 'rails', '3.2.19'
gem 'sqlite3'
gem 'normalizr'

class Foo < ActiveRecord::Base
  normalize :bar
end

class Baz < Foo
end

$ rails c
Loading development environment (Rails 3.2.19)
2.1.2 :001 > b = Baz.new
 => #<Baz id: nil, bar: nil, created_at: nil, updated_at: nil> 
2.1.2 :002 > b.bar = '  Text'
 => "  Text" 
2.1.2 :003 > b.bar
 => "Text"
@funglaub

I'm sorry, my actual code is a little bit more complicated actually and I messed up testing. However it's not working with this setup:

$ cat Gemfile app/models/foo.rb app/models/baz.rb
source 'https://rubygems.org'

gem 'rails', '3.2.19'
gem 'sqlite3'
gem 'normalizr'

class Foo < ActiveRecord::Base
  self.abstract_class = true

  normalize :bar
end

class Baz < Foo
  self.table_name = 'bazs'
end

$ rails c
> b = Baz.new
=> #<Baz:0x00000109008980> {
            :id => nil,
           :bar => nil,
    :created_at => nil,
    :updated_at => nil
}
> b.bar = 'foo'
NoMethodError: super: no superclass method `bar=' for #<Baz id: nil, bar: nil, created_at: nil, updated_at: nil>
from /Users/funglaub/.rvm/gems/ruby-2.1.1/gems/activemodel-3.2.19/lib/active_model/attribute_methods.rb:404:in `method_missing'
>
@funglaub
funglaub commented Nov 9, 2015

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.