Topic: Controller / Model refactoring

Hi guys...

I have a very simple question. I have the following controller snippet

def new
  @model = Product.new(params[:model])
  @model.wholesaler_id = params[:model][:wholesaler_id].to_i
  @model.categeory = "Default"
  if request.post? and @model.save
    @model.tag_with(@model.tagging_tags || "") if @config[:tags][:status]
    flash[:success] = "Complete"
  end
end

Is there anyway that the default values for wholesaler_id and category can be set in the product model instead of the way I did it... This might be the correct way I'm doing it, but I'm sure there is a much more elegant approach using the model

Thanx in advance guys

Re: Controller / Model refactoring

I think you can set defaults in your migrations, so in your create table columns e.g:

t.belongs_to :wholesaler, :default => 50
t.string :category, :default => "Default"

ThinkRefresh - Free Rails Techniques Screencasts

Re: Controller / Model refactoring

Ty for the reply Rare... But unfortunatly the "default" value varies from entry to entry. Take for example logging the user_id of the user that has made the modification to the entry or of the user that created it... this is what I'm currently doing

def new
  @model = Product.new(params[:model])
  @model.created_by = @current_user.id
  if request.post? and @model.save
    flash[:success] = "Complete"
  end
end

Re: Controller / Model refactoring

Ah I see... I don't think you can set dynamic defaults for models. Sorry chap.

ThinkRefresh - Free Rails Techniques Screencasts