I've got a Model that has fields username, data, tags, date, votes. I've form using form_for that produces a brand new item and puts it in to the database. However, as possible guess I would like the votes area to equal and also the date area to equal the present date when it's place into the database. Where and how would I set/apply these values towards the item?

I'm able to get results with hidden fields within the form but this includes apparent issues (someone could set the votes area to some massive number).

Only use a default value zero, for votes within the db, make use of the automatic timestamps(created_at) rather than date, and also have fields within the form just for the parameters you'll set. Be sure to safeguard the sensitive characteristics.

class CreateModels < ActiveRecord::Migration
  def up
    create_table :models do |t|
      t.string :username
      t.text :data
      t.string :tags
      t.integer :votes, :default => 0

      t.timestamps # this will give you two automatic fields: created_at and updated_at
    end
  end
  …
end

class Model < ActiveRecord::Base
  attr_protected :votes #so that it cannot be set by mass assignment
  …
end

One easy strategy is to create the default values around the create action around the controller.

@model = Model.new(params[:model])
@model.votes = 1
@model.date = Time.now
@model.save

One other way, and much more clean too, is to produce a method within the Model.

class Model

def new_default(model)
  model = Model.new(model)
  model.votes = 1
  model.date = Time.now
end

So in your soul controller you'll have:

@model = Model.new_default(params[:model])

if @model.save
   render something
else
   render something_else
end

Here is a screencast on safeguarding characteristics: http://railscasts.com/episodes/26-hackers-love-mass-assignment

Rails models include automatic timestamping automatically, produced_at and up-to-date_at fields would be the names for that characteristics. It's not necessary to be worried about setting them, which will rails handle for you personally.

I'm able to make sure DB constraints approach is among the best. But it's not necessarily possible for use. Assume there's just one table inheritance and different default value per child model is needed. Than I suggest to place this right into a model. Allow me to give a good example:

class ChildModel < Model
  after_initialize :set_defaults
  private
  def set_defaults
    self.allowed_votes_per_person = 10 if self.new_record?
  end 
end

By utilizing :after_initialize callback there's you don't need to create and don't forget to call :new_default like techniques and so forth. It sets needed default values, but stays inconspicuous in the interface perspective when one calls ChildModel.new

I've no privilage to comment to others solutions yet but possess a related question to Nerian, so am writing this. I'm utilizing a code like

before_create : create_rest
def create_rest
  15.times do
    self.players.build({:name => Rnlist.order("rand()").first.raname,
                        :cost => 140+rand(40),
                        :coop => rand(3)})
  end
end

within the model user.rb. This code works perfectly fine and produces 15 gamers with various names, etc. If however I am attempting to place the characteristics in to the player model how you suggest (using create method within the gamers_controller), the self.gamers.build method produces 15 gamers with empty characteristics without giving any error. I figured that using the code with new method rather than create in gamers_controller could solve this but it makes sense exactly the same. Can self.gamers.build method make the problem? I checked the Ruby API for other people.develop() as well as Vinhboy.com blog, but could not solve this problem.