ruby on rails - Random default value for integer in database for each instance? -
i'm using system voting content in rails app: https://github.com/twitter/activerecord-reputation-system
is there way make default score votable item random number each instance.
if store rand(5..12) pick random default value 1 time, how random default value every different row or field?
create_table "rs_evaluations", :force => true |t| t.string "reputation_name" t.integer "source_id" t.string "source_type" t.integer "target_id" t.string "target_type" t.float "value", :default => 0.0 t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end
use before_create filter.
class rsevaluation < activerecord::base before_create :update_value def update_value self.value = rand(5..12) end end however; since evaluation not own model, 1 library, try opening class , patching it:
module reputationsystem class evaluation < activerecord::base before_create :update_value def update_value self.value = rand(5..12) end end end this placed in config/initializers folder.
Comments
Post a Comment