rails / protected_attributes

Protect attributes from mass-assignment in ActiveRecord models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ArgumentError: wrong number of arguments (2 for 1)

kenips opened this issue · comments

In lib/active_record/mass_assignment_security/core.rb line 15:

assign_attributes(attributes, options) if attributes

raises ArgumentError: wrong number of arguments (2 for 1)

as assign_attributes only takes one argument as per https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L14

Could you provide your usage and the full stack trace?

A sample application would be great too.

As a side note, protected_attributes should be overriding that to accept 2 args.

Since we have not heard from you in 1 month I'll close this issue.

Feel free to ask to reopen if you are still facing this issue and can give us the reproduction steps

Sorry - forgot about this. Doesn't seem to be reproducing this anymore. Will definitely update if I come across again. Thanks @rafaelfranca!

I have a table "settings" with only two fields: key, value. And when I try to create new Setting I get the following error:
Ruby 2.1.2
Rails 4.1.4

Error trace

2.1.2 :009 > Setting.find_or_create_by(key: 'Value')
D, [2014-07-22T13:03:50.929430 #11698] DEBUG -- :   Setting Load (324.9ms)  SELECT  "settings".* FROM "settings"  WHERE "settings"."key" = 'Value' LIMIT 1
ArgumentError: wrong number of arguments (2 for 1)
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:14:in `assign_attributes'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/attr_encrypted-1.3.2/lib/attr_encrypted/adapters/active_record.rb:17:in `perform_attribute_assignment'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/attr_encrypted-1.3.2/lib/attr_encrypted/adapters/active_record.rb:24:in `assign_attributes_with_attr_encrypted'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/protected_attributes-1.0.8/lib/active_record/mass_assignment_security/core.rb:8:in `init_attributes'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/core.rb:198:in `initialize'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/inheritance.rb:30:in `new'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/inheritance.rb:30:in `new'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/protected_attributes-1.0.8/lib/active_record/mass_assignment_security/persistence.rb:45:in `create'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/relation.rb:132:in `block in create'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/relation.rb:285:in `scoping'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/relation.rb:132:in `create'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/relation.rb:202:in `find_or_create_by'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/querying.rb:6:in `find_or_create_by'
    from (irb):9
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/mandeep/.rvm/gems/ruby-2.1.2/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
2.1.2 :010 > 

This is a problem with the attr_encrypted gem, please report it at their repository 😄

It appears that the attr_encrypted gem was activated before protected_attributes, so it held onto the old definition of assign_attributes through alias method chain. Ensuring protected_attributes is required (if used) before the gem is activated would probably resolve the issue.

Agree with @chancancode. Just defining protected_attributes before attr_encrypted in our Gemfile fixed it for us. With a comment to explain why the hell we need to do this! 😄