Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acts_as_votable_options somehow became nil #193

Open
Decoydoll opened this issue Mar 1, 2019 · 5 comments
Open

acts_as_votable_options somehow became nil #193

Decoydoll opened this issue Mar 1, 2019 · 5 comments

Comments

@Decoydoll
Copy link

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?

@ouabing
Copy link

ouabing commented May 30, 2019

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

@fuuman
Copy link

fuuman commented May 11, 2020

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

@ryanto
Copy link
Owner

ryanto commented Dec 19, 2020

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

@marcodetoni
Copy link

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.

@moseedat
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants