默认范围内的参数数量(0表示1)错误

时间:2014-04-17 05:21:35

标签: ruby-on-rails

我一直收到ArgumentError: wrong number of arguments (0 for 1) default_scope的错误default_scope { where("#{table_name}.tenant_id IS NULL") }

它一直给我这个错误,我不明白为什么。我的用户模型中有默认范围。

更新

如果使用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'

hash位于我的用户模型中,如下所示。

 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

如果使用rails服务器输出错误:

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?'

相关的rails方法:

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

2 个答案:

答案 0 :(得分:1)

你应该避免覆盖像Object#hash这样的Ruby核心方法,同时考虑到Object#hash是Ruby中必不可少的方法。来自文档:

  

为此对象生成Fixnum哈希值。这个功能必须有   a.eql?(b)暗示a.hash == b.hash。

的属性      

哈希值与eql一起使用?由Hash类来确定   如果两个对象引用相同的哈希键。任何哈希值   超过Fixnum的容量将在被使用前被截断。

     

对象的哈希值在调用之间可能不相同   或者ruby的实现。如果你需要一个稳定的标识符   您将需要生成一个ruby调用和实现   使用自定义方法。

如果你真的必须覆盖核心方法,你应该保证他们的功能并且不要改变他们的论点:否则你的应用会遇到意想不到的行为,比如你提到的错误。

答案 1 :(得分:0)

我认为您没有table_name

这样的变量或方法

我认为这会奏效:

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

使用callable objects作为范围(他们正在等待它)更清晰,所以最好在这里使用lambda:

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") }