If you want to get random object 1 per day, so you should store it somewhere. You can store it:
- in separate file
- in separate model
- in the same model add rndm field
Lets implement the last one. It is quite easy. Imagine, that your Model
called User
. First, let's add new date field rndm
:
rails g migration add_rndm_to_user rndm:date
rake db:migrate
Now we need to add some methods to your User
model
class User < ActiveRecord::Base
def self.random
rndm = find_by_rndm Date.today
unless rndm
update_all :rndm => nil
rndm = self.order('rand()').first
rndm.update_attribute :rndm, Date.today
end
rndm
end
end
so now you can call User.random
from your controller