rails / protected_attributes

Protect attributes from mass-assignment in ActiveRecord models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`method_missing': undefined method `whitelist_attributes=' for ActiveRecord::Base:Class (NoMethodError)

alindeman opened this issue · comments

  1. Create a new app with Rails 3.2.8
  2. Upgrade it to Rails 4:
# Gemfile
gem 'rails', github: 'rails/rails'
gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders'
gem 'journey', github: 'rails/journey'
  1. Add protected_attributes:
# Gemfile
gem 'protected_attributes', github: 'rails/protected_attributes'
  1. Attempt to run bundle exec rails console:
/Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activerecord/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `whitelist_attributes=' for ActiveRecord::Base:Class (NoMethodError)
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activerecord/lib/active_record/railtie.rb:96:in `block (3 levels) in <class:Railtie>'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activerecord/lib/active_record/railtie.rb:95:in `each'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activerecord/lib/active_record/railtie.rb:95:in `block (2 levels) in <class:Railtie>'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activesupport/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activesupport/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activesupport/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activesupport/lib/active_support/lazy_load_hooks.rb:27:in `each'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activesupport/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/activerecord/lib/active_record/railtie.rb:94:in `block in <class:Railtie>'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/initializable.rb:30:in `instance_exec'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/initializable.rb:30:in `run'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:150:in `block in tsort_each'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:219:in `each_strongly_connected_component_from'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:182:in `block in each_strongly_connected_component'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:180:in `each'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:180:in `each_strongly_connected_component'
        from /Users/alindeman/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/tsort.rb:148:in `tsort_each'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/initializable.rb:54:in `run_initializers'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/application.rb:185:in `initialize!'
        from /Users/alindeman/.rvm/gems/ruby-1.9.3-p286/bundler/gems/rails-b1fe78e0cb81/railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
        from /Users/alindeman/workspace/upgradedrails3app/config/environment.rb:5:in `<top (required)>'

Since you upgrade a Rails 3.2.8 application to 4.0.0.beta you have this line in your application.rb.

That configuration was removed in 4.0.0.beta and this is why you are getting this error. Just remove it.

However we need to fix it to not blow this error

@rafaelfranca question: shouldn't whitelist_attributes be available if protected_attributes is installed, the same way it is in 3.2?

@carlosantoniodasilva yes. We need to fix it, but I posted a workaround to the guy continue to work.

Thanks! I definitely know how to work around it, but reading the code seems to imply that the option should work:

@rafaelfranca @carlosantoniodasilva this is going to be the first thing that anyone upgrading a Rails 3.2 app is going to see - it needs fixing in two ways:

  1. When protected_attributes isn't installed it needs to show a useful warning/deprecation message about either migrating to strong_parameters or installing protected_attributes and carry on without terminating. I know you could consider it a security issue but I think it's safe to assume that no-one will upgrade to Rails 4 and then deploy without running rails console at least once.
  2. When protected_attributes is installed it needs to work. 😄

I will be checking this tomorrow, I'm not sure how/when this stop working 😁

Yeah, agreed with @pixeltrix comments about it. With the gem installed, it should just work, otherwise we may need to handle it on the Rails side and ignore the option, with a warning. Sounds good.

Yes, I agree. It is in my TODO list, but if you want to help me and do it, I would be thankful. My TODO list is huge.

Well if @guilleiguaran makes it work, I'll handle the deprecation warning in Rails itself.

having this problem now: rails/rails#13740