Comment mettre en place un compteur cache dans Rails?
-
23-10-2019 - |
Question
J'ai un contrôleur de postes et un contrôleur de commentaires. Poster a de nombreux commentaires et les commentaires appartiennent à Post. L'associé est configuré avec l'option counter_cache activée en tant que tel:
#Inside post.rb
has_many :comments
#Inside comment.rb
belongs_to :post, :counter_cache => true
J'ai une colonne de comments_count
dans ma table de posts
qui est par défaut à zéro, en tant que tel:
add_column :posts, :comments_count, :integer, :default => 0
Dans l'action create
de mon contrôleur commentaire, je le code suivant:
def create
@posts = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
if @comment.save
redirect_to root
else
render :action => 'new'
end
end
Mon problème: quand @comment.save
est appelé, je reçois l'erreur suivante:
ArgumentError in CommentsController#create
wrong number of arguments (2 for 0)
Retrait :counter_cache => true
de comment.rb résout complètement le problème, je suis tellement suppose qu'il est la cause de cette erreur vague. Qu'est-ce que j'oublie ici?
Comment puis-je sauvegarder mes commentaires et ont encore des rails prendre soin de mon counter_cache pour mon poste?
Merci!
Mise à jour - trace de demande ci-joint:
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:987:in `update_all'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:987:in `update_counters_without_lock'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/locking/optimistic.rb:176:in `update_counters'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1006:in `increment_counter'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations.rb:1367:in `belongs_to_counter_cache_after_create_for_feed_entry'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:178:in `send'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:178:in `evaluate_method'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:166:in `call'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:93:in `run'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:92:in `each'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:92:in `send'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:92:in `run'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:276:in `run_callbacks'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:344:in `callback'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:267:in `create'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2915:in `create_or_update_without_callbacks'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:250:in `create_or_update'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2573:in `save_without_validation'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1090:in `save_without_dirty'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/dirty.rb:79:in `save_without_transactions'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in `send'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:182:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in `save'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in `save'
/Users/yuval/Sites/rails/blog/app/controllers/comments_controller.rb:6:in `create'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in `perform_action'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `process_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:in `call'
Remarquez la seule ligne qui ne doit pas faire directement avec le framework Rails est:
/Users/yuval/Sites/rails/blog/app/controllers/comments_controller.rb:6:in `create'
Ligne 6 est le suivant:
if @comment.save
EDIT 2:. @posts
/ @post
est une faute de frappe sur ma fin lors de l'écriture d'un exemple simplifié pour StackOverflow, ma demande réelle ne confondez pas
@comment
Inspecter, il semble être tout à fait valable - il retourne exactement ce que je pense à - une nouvelle instance de commentaire avec toutes les informations que je suis passé à avec build
. Il souffle que lorsque la méthode de save
est appelé là-dessus.
Merci pour toutes les suggestions jusqu'à présent. D'autres idées?
La solution
J'ai couru à travers un échantillon rails application avec votre code et tout a bien fonctionné pour moi.
Je suggère le débogage d'un peu plus comme hurikhan77 suggère-et voir si elle est juste la question de @posts / @post que Dain a suggéré.
De plus, essayez de créer un poste et un commentaire dans la console avec un contenu très simple pour voir si elle fonctionne.
$ ruby script/console
# add whatever fields are necessary to create
> @p = Post.create(:title => "TestPost1")
# => #<Post id: 3, ...
# again, add whatever is necessary to create
> @c = @p.comments.create(:comment => "TestComment1")
# => #<Comment id: 8, ...
> Post.find(:last).comments_count
# => 1
Voir ce qui vous fait.
/ JP
Autres conseils
Cela pourrait être complètement hors de la marque, mais vous utilisez @posts puis @post?
ArgumentError en CommentsController # créer
mauvais nombre d'arguments (2 à 0)
Vous obtenez cette erreur, car @comment n'est pas l'objet que vous attendez. Essayez de déboguer en insérant:
logger.debug @comment.inspect
Vous verrez quelque chose d'inattendu et il devrait lever au moins un sourcil. Cela devrait être le clou que vous avez attribué le Post.find(...)
à @posts
mais plus tard a essayé de travailler avec @post
.