0

我有一个简单的模型

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

当我尝试删除 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>'

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

4

2 回答 2

1

bullet宝石似乎是问题所在。查看堆栈跟踪的第一行。问题就在这个方法里面。这可能origin_has_cached_counter?是它抱怨的电话。

于 2012-03-21T19:55:38.030 回答
-1

你忘记=>了你的模型并放了一个:不应该在那里的;)它应该是这样的:

class User < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
  has_many :answers, :dependent => :destroy
end
于 2012-03-22T14:19:23.177 回答