Jorge alejandro Mendoza torres
2010-Mar-13 18:20 UTC
How can I conditional the validation on a model validation?
I''m making an application in Ruby, but I create records from text files, which contain basic information to create a record. Then I modify the records from the website, but now validating all fields. What I do is validate some fields when the record is created from the text file, and then validate the other fields which are filled from the website. Here''s an example: # My Table Table name: Person Fields: id, name, address, telephone, mail The text file only contains the id and name. From the website, add the other fields, but I also want to validate. So I have the model: class Person <ActiveRecord:: Base validates_presence_of: id validates_presence_of name end How can I make previous validation from the model? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Mar-13 20:38 UTC
Re: How can I conditional the validation on a model validation?
On 13 March 2010 18:20, Jorge alejandro Mendoza torres <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m making an application in Ruby, but I create records from text files, > which contain basic information to create a record. Then I modify the > records from the website, but now validating all fields. > What I do is validate some fields when the record is created from the > text file, and then validate the other fields which are filled from the > website. > Here''s an example: > > # My Table > Table name: Person > Fields: id, name, address, telephone, mail > > The text file only contains the id and name. From the website, add the > other fields, but I also want to validate. > > So I have the model: > > class Person <ActiveRecord:: Base > validates_presence_of: id > validates_presence_of nameIs this what you want? validates_presence_of :somethingelse, :on => :update See the docs for validations at http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Anthony Gardner
2010-Mar-14 11:29 UTC
Re: How can I conditional the validation on a model validation?
If you''re asking "how do I avoid the validation check on name and id when updating", then you should look at :on => params for validation. validates_presence_of :id, :name, :on => :create I think everything is clearly explained in the docs. http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html <http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html> On 13 March 2010 19:20, Jorge alejandro Mendoza torres <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m making an application in Ruby, but I create records from text files, > which contain basic information to create a record. Then I modify the > records from the website, but now validating all fields. > What I do is validate some fields when the record is created from the > text file, and then validate the other fields which are filled from the > website. > Here''s an example: > > # My Table > Table name: Person > Fields: id, name, address, telephone, mail > > The text file only contains the id and name. From the website, add the > other fields, but I also want to validate. > > So I have the model: > > class Person <ActiveRecord:: Base > validates_presence_of: id > validates_presence_of name > end > > How can I make previous validation from the model? > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 100% naturally selected. 0% designed. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.