Validates


#1

If I have a table: users(id, name, address, city)

How can I validate:
Name must be entered, but address and city can be null, but if there is
address, there also must be a city. So city can’t be null, if address is
not, but bouth address and city can be null.

validates_presence_of name
validates ???


#2

Jo Jo wrote:

If I have a table: users(id, name, address, city)

How can I validate:
Name must be entered, but address and city can be null, but if there is
address, there also must be a city. So city can’t be null, if address is
not, but bouth address and city can be null.

validates_presence_of name
validates ???

Hi,
Try with following code.

def validate
errors.add(:city,“City should not be empty”) if city==nil and
address!=nil
end


#3

class User
validates_presence_of :name
validates_presence_of :city, :if => :address
validates_presence_of :address, :if => :city
end

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)