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 ???? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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) On Tue, Feb 24, 2009 at 5:56 AM, Jo Jo <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 ???? > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---