Rails: How to set different validation rules on creation and update

I have a text field that can be empty when created, but not when updated.

How can I do that in rails: Different validation rules depending on action?

The idea behind this, is to allow an admin to create a blank issue ticket, to be filled by a user.

Here is my original model (issue.rb):

class Issue < ActiveRecord::Base

attr_accessible :content, :status

validates :content, :presence => true, :length => { :maximum => 2048 }
validates :status, :inclusion => { :in => %w(WAITING REJECTED ON OFF) }

belongs_to :user

end

How can I set :presence => true of :content only when updating, but not when creating?

Thanks in advance.

Answers


You can use :on => :create in your validation statement.

Like in this question.


Need Your Help

Calculating previous chance figure for use in chi square test

python python-2.7 csv chi-squared

SO, I am using a script to calculate the number of occurrences of times a person appears in the list on dates prior to the date specified in the row and a 1 occurs in column 6, and also calculates ...

What are the most important POSIX functions not available in Android?

android linux android-ndk posix

Im ahead of porting a large c++ Project (some sort of Libary Project, it contains absolutly no GUI) to Android (which is actually an Visual C/C++ Project, but it will be ported to Linux as intermid...