rails / protected_attributes

Protect attributes from mass-assignment in ActiveRecord models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rails 4.2-stable has_many through association ArgumentError: wrong number of arguments (2 for 1) when using protected_attributes, method signature mismatch

mcfiredrill opened this issue · comments

I have a problem that seems to be similar to rails/rails#18121

rails: 4-2-stable git
protected_attributes: git master branch

There seems to be another method signature mismatch HasManyThroughAssociation#build_record(https://github.com/rails/protected_attributes/blob/master/lib/active_record/mass_assignment_security/associations.rb#L72), which only takes one argument now, and the subsequent override in protected_attributes(https://github.com/rails/protected_attributes/blob/master/lib/active_record/mass_assignment_security/associations.rb#L72) . Say I have a users table, tasks table, and users_tasks table with has_many :through set up on both sides.

irb(main):001:0> u = User.create name: 'tony'                                                                                                                                                                                                    
=> #<User id: 1, name: nil, created_at: "2015-01-19 07:59:04", updated_at: "2015-01-19 07:59:04">
irb(main):004:0> u.tasks.new name: 'wat'
ArgumentError: wrong number of arguments (2 for 1)
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activerecord/lib/active_record/associations/through_association.rb:95:in `build_record'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/protected_attributes-61e622368d66/lib/active_record/mass_assignment_security/associations.rb:72:in `build_record'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/protected_attributes-61e622368d66/lib/active_record/mass_assignment_security/associations.rb:19:in `build'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/protected_attributes-61e622368d66/lib/active_record/mass_assignment_security/associations.rb:55:in `build'
        from (irb):4
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/railties/lib/rails/commands/console.rb:110:in `start'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/railties/lib/rails/commands/console.rb:9:in `start'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/railties/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/railties/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/railties/lib/rails/commands.rb:17:in `<top (required)>'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:274:in `require'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:274:in `block in require'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:274:in `require'
        from /Users/tony/src/rails_bug/bin/rails:8:in `<top (required)>'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:268:in `load'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:268:in `block in load'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/bundler/gems/rails-26c293e1d018/activesupport/lib/active_support/dependencies.rb:268:in `load'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /Users/tony/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'irb(main):005:0>

I have an example repo with a test that will trigger the error:
https://github.com/mcfiredrill/rails_bug
The test is here:
https://github.com/mcfiredrill/rails_bug/blob/master/test/models/user_test.rb#L7

this error should be resolved by PR #54

Nice, just tried it out, seems to fix the problem. 👍

Have no idea about the code so I can't give any feedback on that.

#54 solves it for me. My specs are still passing on Rails 4.0 and Rails 4.1 as well.

gem "protected_attributes", :github => "rails/protected_attributes"

Any plans for a protected_attributes gem release?

should be fixed by #64