Hello all. I have a problem using validates_inclusion_of. I need to switch off some validations during a migration period, is there a way? I tried to use if, with a static method of one of my models, but the behaviour is something strange It seems that the static method should be in a string to be evaluated. And the order of the elements in the if is counting... :if => (:some_field && ''MyModel.some_static_stuff'' ) -- 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 -~----------~----~----~----~------~----~------~--~---
So... there is no way to do this.... What a framework :( -- 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 -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-13 14:51 UTC
Re: validates_inclusion_of disabling
Yeah, no answers in 4 hours! Rails must suck! I don''t know what you are trying to do. Are you trying to turn off conditions based on a particular condition? I.E. "If a user submits from form A then validate, if a user submits from form B then don''t validate" ? On Mar 13, 9:42 am, Pedro Cardoso <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> So... there is no way to do this.... > > What a framework :( > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Sorry for the stress Andrew, but in my last posts I didn''t get any answers. I''m trying to turn off validations based on config. I have a constant that tells me if I''m runnig a migration, and if I do, some validations should be turned of. Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000940 That link will tell you how to set up a :if statement on a validation. Basically, the :if should be a string (which will be run through an eval), a function (via it''s symbol name), or a Proc... whichever of the 3 it is, it should return true or false when evaluated. validates_inclusion_of :test, :if => ":some_field && MyModel.some_static_stuff" or validates_inclusion_of :test, :if => :is_migration? def is_migration? :some_field && MyModel.some_static_stuff" end or even validates_inclusion_of :test, :if => Proc.new { :some_field && MyModel.some_static_stuff } If your requirements might change (you might add to or take away from the conditions), you''d be best off going with a function, if it''s VERY simple stuff (like what you have here) and it will never change, use a string. If you need access to the particularly object that the validation is being called on, you can use a proc (or a function) that accepts an argument... validates_inclusion_of :name, :if => Proc.new { |user| user.should_have_name? } On 3/13/07, Pedro Cardoso <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Sorry for the stress Andrew, but in my last posts I didn''t get any > answers. > > I''m trying to turn off validations based on config. I have a constant > that tells me if I''m runnig a migration, and if I do, some validations > should be turned of. > > Thanks. > > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Well, it should be simple... My reality: In env[...].rb: MIGRATING = false In My model: def self.validate return !MIGRATING end And the use of the if statement... validates_inclusion_of :objective, :in => Range.new(Date.today, Date.new(y=4712)), :if => ":objective && Requirement.validate_on_mig" ,:message => "bla bla bla" Does not work :( -- 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 -~----------~----~----~----~------~----~------~--~---
In the model it''s not validate but self.perform_validation -- 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 -~----------~----~----~----~------~----~------~--~---
What is the :objective check in the :if supposed to be doing? What is the validate_on_mig function doing? I don''t see you calling the self.perform_validation from anywhere, is it necessary? On 3/13/07, Pedro Cardoso <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > In the model it''s not validate but self.perform_validation > > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Luke Ivers wrote:> What is the :objective check in the :if supposed to be doing? > What is the validate_on_mig function doing? > > I don''t see you calling the self.perform_validation from anywhere, is it > necessary?The :objective is the field from the model. And the self.perform_validation it''s beeing called in the :if, by Requirement.perform_validation The correct validations is: validates_inclusion_of :objective, :in => Range.new(Date.today, Date.new(y=4712)), :if => ":objective && Requirement.validatevalidation _mig" ,:message => "bla bla bla" -- 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 -~----------~----~----~----~------~----~------~--~---
> > The correct validations is: > validates_inclusion_of :objective, :in => Range.new(Date.today, > Date.new(y=4712)), :if => ":objective && Requirement.validatevalidation > _mig" > ,:message => "bla bla bla"From what I can tell, the correct validation is validates_inclusion_of :object, :in => Range.new(Date.today, Date.new(y=4712)), :if => "Requirement.perform_validation", :message => "blah blah blah" You don''t need to validate the field in the validation test for the field... you''re completely missing the point of the :if statement. If you absolutely HAVE to have that in there, I would change the :if to :if => Proc.new { |m| m.objective && Requirement.perform_validation } I still have absolutely no clue where that whole validatevalidation_mig stuff is coming from... but I''m pretty sure what I said is what you''re looking for. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> From what I can tell, the correct validation is > validates_inclusion_of :object, :in => Range.new(Date.today, > Date.new(y=4712)), > :if => "Requirement.perform_validation", :message => "blah blah blah" > > You don''t need to validate the field in the validation test for the > field... > you''re completely missing the point of the :if statement. > If you absolutely HAVE to have that in there, I would change the :if to > :if => Proc.new { |m| m.objective && Requirement.perform_validation } > > I still have absolutely no clue where that whole validatevalidation_mig > stuff is coming from... but I''m pretty sure what I said is what you''re > looking for.Thanks for the reply Luke! I have the validation of the fieldin the if statement because i wan''t to validate if the field is in the correct Range if the user inserts something, if he does not, the message is different... that validation_on mig is the Requirement.perform_validation... I changed the method name... sorry for the mess..., I''m gonna try with Proc.new... News in some minutes -- 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 -~----------~----~----~----~------~----~------~--~---
Luke, you''re absolutely right! It works fine! If I change in my config the MIGRATION to false, the validations is performed! Thanks a lot! -- 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 -~----------~----~----~----~------~----~------~--~---
On 3/13/07, Pedro Cardoso <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Luke, > > you''re absolutely right! > > It works fine! > > If I change in my config the MIGRATION to false, the validations is > performed! > > Thanks a lot!You''re welcome :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Luke Ivers wrote:> On 3/13/07, Pedro Cardoso <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Thanks a lot! > > > You''re welcome :)And for a little explanation, if I''m not wrong, I''m making a new procedure on the fly, the m is my own instance and the other part of the && is a call to my own static method... great :) -- 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 -~----------~----~----~----~------~----~------~--~---