Gugubaight
Gugubaight

Reputation: 187

Rails 4, SQLite 3: Acts as Votable - Sort for upvotes last week

I'd like to display the most upvoted items from the last 7 days.
That way I am able to get the most upvoted items (all time):

@items = Item.all.order(cached_votes_up: :desc)

item.rb

class Recipe < ActiveRecord::Base

    belongs_to :user
    acts_as_votable

end

Is there a way to get the most upvoted Items during the last week?

Any help is appreciated. Thanks.

Upvotes: 1

Views: 112

Answers (1)

Melvin
Melvin

Reputation: 376

I am doing this based on what I remembered when I used the gem a while ago.

You have two tables, items and votes, votes are created by default if a acts_as_votable gem is used.

To join the both the table together
@join_table = Item.joins('INNER JOIN votes ON votes.votable_id = items.id')

//I have not used cached_votes_up when I used the gem. I have no idea what does it store and what is it for. If it works, continue below.

After you joined the table, now we need to filter out the item that are rated for the past 7 days.
@filter_table = @join_table.order(cached_votes_up: :desc).where(:updated_at => 7.day.ago..Time.now)

You may need to modify slightly as I do not have the exact table attributes. Let me know if this is what you want.

Upvotes: 2

Related Questions