"The daylight on the website"
I'm building a "Director of Day" in ruby on rails. We need to get the tape off in a random way and not change in one day.
You can make an accidental recipe:
@recipe_day = Recipe.order("RANDOM()").first
But with every reboot of the page, naturally, it will come out differently. How do you make sure he doesn't change all day?
Add the field to through migration:
rails g migration AddTopToRecipes top:boolean
To avoid three conditions (true, false, nil) it is better to:
class AddTopToRecipes < ActiveRecord::Migration def change add_column :recipes, :top, :boolean, null: false, default: false end end
Don't forget to migration:
class Recipe < ActiveRecord::Base # Return the current recipe of the day def self.recipe_of_the_day self.find_by_top(true) end
Set up the recipe of the day
current_recipe = self.recipe_of_the_day
recipe_ids = self.all( :select => "id", :conditions => ['id != ?', current_recipe.id] ) next_recipe = self.find(recipe_ids[rand(recipe_ids.length)]) current_recipe.update_attribute(:top, false) next_recipe.update_attribute(:top, true)
Use the recipe once a day. http://github.com/javan/whenever ♪
Set the target on the schedule:
every 1.day do
ps: The code is better re-established.