Object.destroy生成ArgumentError:错误的参数个数(1表示0)

时间:2012-03-21 19:47:02

标签: ruby-on-rails ruby activerecord active-relation

我有一个简单的模型

class User < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :answers, dependent: :destroy
end

当我尝试删除whith User.first.destroy(在控制台上)时,我得到:ArgumentError:错误的参数数量(1表示0)。

细节:

u.destroy
Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = 4
SQL (14.6ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 4]]
SQL (0.1ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 5]]
SQL (0.0ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 6]]
ArgumentError: wrong number of arguments (1 for 0)
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/bullet-2.1.0/lib/bullet/active_record31.rb:88:in `has_cached_counter?'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/has_many_association.rb:61:in `update_counter'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/has_many_association.rb:90:in `delete_records'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:452:in `block in    delete_or_destroy'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:147:in `block in transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:146:in `transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:449:in `delete_or_destroy'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:220:in `delete'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_association.rb:155:in `delete_all'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/collection_proxy.rb:54:in `delete_all'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/associations/builder/has_many.rb:42:in `block in   define_destroy_dependency_method'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:395:in `_run_destroy_callbacks'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `destroy'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `block in destroy'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in  `transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `destroy'
from (irb):2
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/matreyes/.rvm/gems/ruby-1.9.3-p0@curso/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我需要帮助..我已经尝试了几个小时 谢谢!

2 个答案:

答案 0 :(得分:1)

bullet宝石似乎是个问题。查看堆栈跟踪的第一行。问题出在this method内。这可能是它抱怨的origin_has_cached_counter?电话。

答案 1 :(得分:-1)

您忘记了模型中的=>并放置了不应该存在的:;) 它应该是这样的:

class User < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
  has_many :answers, :dependent => :destroy
end