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

caching not working as expected #82

Closed
thatfunkymunki opened this issue Jun 9, 2014 · 13 comments
Closed

caching not working as expected #82

thatfunkymunki opened this issue Jun 9, 2014 · 13 comments

Comments

@thatfunkymunki
Copy link

I ran the migration as shown in the readme, but whenever I do object.get_likes.size it still queries the votes table without hitting the cache. Additionally the cache columns are not automatically updating with new votes. Are there any procedures to perform?

Score: <%[email protected]_likes.size - @card.get_dislikes.size%>
Cached Score: <%[email protected]_votes_up.size - @card.cached_votes_down.size%>

@ryanto
Copy link
Owner

ryanto commented Jun 9, 2014

Open db/schema.rb and paste your the "create_tables 'votes'...." section.

@thatfunkymunki
Copy link
Author

create_table "votes", force: true do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "voter_id"
t.string "voter_type"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
end

@ryanto
Copy link
Owner

ryanto commented Jun 9, 2014

You don't have any of the cache columns on your table. Please see this page: https://github.com/ryanto/acts_as_votable#caching

@ryanto ryanto closed this as completed Jun 9, 2014
@thatfunkymunki
Copy link
Author

I have the cached columns under my other model as stated in the link given:

create_table "cards", force: true do |t|
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "cached_votes_total", default: 0
t.integer "cached_votes_score", default: 0
t.integer "cached_votes_up", default: 0
t.integer "cached_votes_down", default: 0
t.integer "cached_weighted_score", default: 0
t.integer "cached_weighted_total", default: 0
end

@ryanto
Copy link
Owner

ryanto commented Jun 13, 2014

Whoops, sorry about that.

What version of ruby and what version of rails?

@ryanto ryanto reopened this Jun 13, 2014
@thatfunkymunki
Copy link
Author

action@cerebral-depravity-120411:/cerebral-depravity(master_)$ rails version
Rails 4.1.0
action@cerebral-depravity-120411:
/cerebral-depravity(master_)$ ruby --version
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]

@vietqhoang
Copy link
Contributor

What happens when you try the following?

@card.count_votes_up and @card.count_votes_down

Looking at the votable.rb, it looks like @card.get_likes and @card.get_dislikes will always hit the Vote table. Whereas the above will hit the cache columns of the votable model.

This still doesn't explain why your caching is not updating though...

Did you run the following command force caching of existing votes for the votable model? I am assuming the model is Card.
Card.find_each(&:update_cached_votes)

Also you don't need to call .size on @card.cached_votes_up and @card.cached_votes_down, since the return is an integer.

@thatfunkymunki
Copy link
Author

Yep, I ran the

Card.find_each(&:update_cached_votes)

in the migration. Then @card.cached_votes_up still gives me 0. I can't see the column updating either.

@thatfunkymunki
Copy link
Author

I guess I was doing something wrong because it is working now. Thanks for your support!!!

@clairity
Copy link

clairity commented Dec 7, 2016

if anyone else runs into this issue of the cache seemingly not working, a restart of the rails server just might fix it. =)

@vietqhoang
Copy link
Contributor

vietqhoang commented Feb 25, 2021

Hi,
i have a similar problem, maybe someone can help.
Displaying the cached_weighted score in show, there is no problem.
BUT as soon as i try to order/reorder int wont work
i tried running this:
@movie_suggestions.order(:cached_weighted_score => :desc)
@movie_suggestions.order('cached_weighted_score DESC')
@movie_suggestions.reorder(:cached_weighted_score => :desc)
@movie_suggestions.reorder('cached_weighted_score DESC')
@movie_suggestions.reorder(:weighted_score => :desc)
@movie_suggestions.order(:weighted_score => :desc)
all of this doesnt work, although the value of a single suggestion is displayed correctly in view using this:
suggestion.weighted_score
any suggestions on why it wont work?

What error output are you getting? The information will help in figuring out the source of your issue.

I am going off the assumption your results are not being ordered correctly. Do you have a default scope set on your MovieSuggestion model? Have you tried chaining to_sql to any of your calls to see what the SQL output is like? It may reveal what the issue is.

@YouckFou46
Copy link

The problem was in the migration, somehow. Try and rewrite it that might help

@jDeppen
Copy link

jDeppen commented Nov 3, 2022

The issue for me was that I was only utilizing vote_weight as star rating, so I left vote_flag: nil. I needed to set vote_flag in order for the cache to update these:

cached_weighted_average # vote_flag must be true for the vote to count
cached_votes_total # vote_flag can't be nil or the vote won't count

note: cached_weighted_total worked fine with vote_flag: nil
(these are the only three cache columns I'm using, so I can't speak to the others)

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