Object.destroy генерирует ArgumentError: неверное количество аргументов (1 для 0) - PullRequest
0 голосов
/ 21 марта 2012

У меня есть простая модель, где

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

Мне нужна помощь .. Я пытался часами Спасибо!

Ответы [ 2 ]

1 голос
/ 21 марта 2012

Кажется, проблема в bullet драгоценном камне. Посмотрите на первую строку вашей трассировки стека. Проблема внутри этот метод . Это, вероятно, origin_has_cached_counter? звонок, на который он жалуется.

0 голосов
/ 22 марта 2012

Вы забыли => в своей модели и поместили :, которого там быть не должно;) Должно быть так:

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