Wrong number of arguments (0 for 1) in default scope

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

I keep getting the error ArgumentError: wrong number of arguments (0 for 1) for my default_scope which is default_scope { where("#{table_name}.tenant_id IS NULL") }

It keeps giving me this error and I don't understand why. I have the default scope in my users model.

Update:

Error output if using rails console:

ArgumentError: wrong number of arguments (0 for 1)     from /home/evan/Apps/demo-application/app/models/user.rb:18:in `hash'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping.rb:64:in `value_for'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `public_send'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `block in method_missing'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:123:in `ignore_default_scope?'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:134:in `evaluate_default_scope'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:110:in `build_default_scope'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:554:in `with_default_scope'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:582:in `exec_queries'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:471:in `load'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:220:in `to_a'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:573:in `inspect'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'     from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'     from bin/rails:4:in `require' 

The hash is in my user model as below.

 def User.new_remember_token      SecureRandom.urlsafe_base64  end   def User.hash(token)      Digest::SHA1.hexdigest(token.to_s)  end   private       def create_remember_token          self.remember_token = User.hash(User.new_remember_token)      end 

Error output if using rails server:

ArgumentError - wrong number of arguments (0 for 1):   activerecord (4.0.4) lib/active_record/scoping.rb:70:in `set_value_for'   activesupport (4.0.4) lib/active_support/per_thread_registry.rb:40:in `block in method_missing'   activerecord (4.0.4) lib/active_record/scoping/default.rb:127:in `ignore_default_scope='   activerecord (4.0.4) lib/active_record/scoping/default.rb:140:in `ensure in evaluate_default_scope'   activerecord (4.0.4) lib/active_record/scoping/default.rb:140:in `evaluate_default_scope'   activerecord (4.0.4) lib/active_record/scoping/default.rb:110:in `build_default_scope'   activerecord (4.0.4) lib/active_record/relation.rb:554:in `with_default_scope'   activerecord (4.0.4) lib/active_record/relation.rb:582:in `exec_queries'   activerecord (4.0.4) lib/active_record/relation.rb:471:in `load'   activerecord (4.0.4) lib/active_record/relation.rb:220:in `to_a'   activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:316:in `find_take'   activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:66:in `take'   activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:49:in `find_by'   activerecord (4.0.4) lib/active_record/querying.rb:6:in `find_by'   app/helpers/sessions_helper.rb:16:in `current_user'   app/helpers/sessions_helper.rb:19:in `signed_in?' 

And the relevant rails methods:

def ignore_default_scope? # :nodoc:      ScopeRegistry.value_for(:ignore_default_scope, self) end  def ignore_default_scope=(ignore) # :nodoc:      ScopeRegistry.set_value_for(:ignore_default_scope, self, ignore) end 

回答1:

You should avoid overwriting Ruby core methods like Object#hash, also considering that Object#hash is an essential method in Ruby. From the docs:

Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash.

The hash value is used along with eql? by the Hash class to determine if two objects reference the same hash key. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.

The hash value for an object may not be identical across invocations or implementations of ruby. If you need a stable identifier across ruby invocations and implementations you will need to generate one with a custom method.

If you really have to overwrite core methods you should guarantee their functionality and do not change their arguments: you will get unexpected behaviours all over your app otherwise, like the error you're referring.



回答2:

I think that you have not such a variable or method as table_name

I think this will work:

table name = "users" default_scope { where("#{table_name}.tenant_id IS NULL") } 

Also it is cleaner to use callable objects for scopes (they are waiting for it), so it is better to use lambda here:

table name = "users" default_scope ->{ where("#{table_name}.tenant_id IS NULL") }  # or old syntax:  default_scope lambda { where("#{table_name}.tenant_id IS NULL") } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!