Domanda

I'm having problems with the thumbs_up gem.

Here is how my app works:

Pictures act as voteable, users act as voters. They can like and dislike pictures (so the plusminus count can go negative, too). In the pictures database: user_id, :name, :image, :fame. Fame is a boolean and is by default false.

And here is what I want to do now:

Every 24 hours the picture with the highest plusminus tally gets its :fame value changed from false to true, thereby disappearing from the picture library (pictures#index) and appearing on pictures#highest. Only this one picture, changing every 24 hours.

Basically I don't know how to change the boolean of the most liked picture to true every 24 hours, and how to select the picture with the most likes. Any code is appreciated.

È stato utile?

Soluzione

Your problem is not the thumbs_up gem.

Your problem is that you don't know how to schedule periodic tasks.

A tool that you can use for this is the Whenever gem. It lets you do stuff like this:

# in whenever
every 24.hours do
  rake "pictures:set_fame"
end

# lib/tasks/my_task.rake
namespace :pictures do
  task :set_fame => :environment do
    # N.B.: I haven't had the chance to test this query.
    Picture.where(fame: true).first.update_attribute(:fame, false)
    Picture.order('tally DESC').first.update_attribute(:fame, true)
  end
end 

Altri suggerimenti

to change the boolean value do

@pic.update_attribute(:fame, true)

for every 24 hours try using the whenever gem

https://github.com/javan/whenever

http://railscasts.com/episodes/164-cron-in-ruby

to select picture with most votes try this

@best_pic = Picture.all.sort{|a,b| b.votes.count <=> a.votes.count}.first
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top