Eu tenho um modelo simples onde
class User < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :answers, dependent: :destroy
end
Quando eu tento excluir whith User.first.destroy (no console) eu tenho: ArgumentError: número errado de argumentos (1 para 0).
Os detalhes:
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>"
Eu preciso de ajuda .. Eu tenho tentado por horas Obrigado!
Respostas:
1 para resposta № 1o bullet
gem parece ser o problema. Olhe para a primeira linha do seu stacktrace. A questão está dentro este método. É provavelmente o origin_has_cached_counter?
Chame que está reclamando.
-1 para resposta № 2
Você esqueceu =>
em seu modelo e colocar um :
que não deveria estar lá; Deve ser assim:
class User < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :answers, :dependent => :destroy
end