ryanto/acts_as_votable

acts_as_votable_options somehow became nil

Opened this issue · 5 comments

Here is my web app environment:

Rails           : 5.2.2
acts_as_votable : 0.12.0

Here is the stack trace that i got:

…cts_as_votable-0.12.0/lib/acts_as_votable/cacheable.rb: 107:in `update_cached_votes'
…/acts_as_votable-0.12.0/lib/acts_as_votable/votable.rb: 101:in `block in vote_by'
…rd/connection_adapters/abstract/database_statements.rb: 259:in `block in transaction'
…ive_record/connection_adapters/abstract/transaction.rb: 239:in `block in within_new_transaction'
       /opt/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb: 214:in `mon_synchronize'
…ive_record/connection_adapters/abstract/transaction.rb: 236:in `within_new_transaction'
…rd/connection_adapters/abstract/database_statements.rb: 259:in `transaction'
…s/activerecord-5.2.2/lib/active_record/transactions.rb: 212:in `transaction'
…/acts_as_votable-0.12.0/lib/acts_as_votable/votable.rb:  96:in `vote_by'
…...............app/controllers/api/votes_controller.rb:  24:in `create'

Here is the error message:

NoMethodError: undefined method `[]' for nil:NilClass

After lookin for the code, here is the part of code that throw that error

self.send(acts_as_votable_options[:cacheable_strategy], updates) if updates.size > 0

which call acts_as_votable_options that is assigned in https://github.com/ryanto/acts_as_votable/blob/599995f7ec5aa0f8a04312768fc956e9003d32d4/lib/acts_as_votable/extenders/votable.rb#L24-#L31
which, per my understanding will never be nil except there is another assignment.

My model only calls acts_as_votable without any args so i assume it will use the default options.

Anyone can explain why this could happen?

Same issue here.

A temporary workaround will be explicitly defining a method in your acts_as_votable model:

acts_as_votable

def acts_as_votable_options
  @acts_as_votable_options ||= { cacheable_strategy: :update_attributes }
end

Same problem here. Whenever i want to down or upvote, I get the exact same error message. Nothing is votable anymore.

Rails: 5.2.3
Version: 0.12.1

Does anyone have an app that can reproduce this issue? That would be helpful here!

Does anyone have an app that can reproduce this issue? That would be helpful here!

Don't have a project to share but the issue is reproducible: the problem seems related to models that implement a single table inheritance, when acts_as_votable is set only on father object.

A workaround working for me is adding acts_as_votable even on child model.

Does anyone have an app that can reproduce this issue? That would be helpful here!

Don't have a project to share but the issue is reproducible: the problem seems related to models that implement a single table inheritance, when acts_as_votable is set only on father object.

A workaround working for me is adding acts_as_votable even on child model.

100%

Had exactly the same issue. Had STI table (Parent class is called Post, child classes were Article, Link etc). Child classes inherit from Parent class.

Had to manually add

acts_as_votable cacheable_strategy: :update_columns

to child classes:

class Posts::Link < Post
  acts_as_votable cacheable_strategy: :update_columns
end

And to the parent class.