# validates\_inclusion\_of disabling

**URL:** <https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179>\
**Category:** rubyonrails-talk\
**Created:** [March 13, 2007, 2:51pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179 "2007-03-13T14:51:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewO](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@AndrewO](https://discuss.rubyonrails.org/u/AndrewO)\
**Post date:** [March 13, 2007, 2:51pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179/1 "2007-03-13T14:51:35Z")

</div>

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" ?

---

<div class="post-metadata">

**Author:** ![Luke\_Ivers](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Luke\_Ivers](https://discuss.rubyonrails.org/u/Luke_Ivers)\
**Post date:** [March 13, 2007, 3:24pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179/2 "2007-03-13T15:24:34Z")

</div>

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000940](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? }

---

<div class="post-metadata">

**Author:** ![Luke\_Ivers](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Luke\_Ivers](https://discuss.rubyonrails.org/u/Luke_Ivers)\
**Post date:** [March 13, 2007, 3:58pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179/3 "2007-03-13T15:58:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Luke\_Ivers](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Luke\_Ivers](https://discuss.rubyonrails.org/u/Luke_Ivers)\
**Post date:** [March 13, 2007, 4:31pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179/4 "2007-03-13T16:31:22Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Luke\_Ivers](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Luke\_Ivers](https://discuss.rubyonrails.org/u/Luke_Ivers)\
**Post date:** [March 13, 2007, 4:46pm UTC](https://discuss.rubyonrails.org/t/validates-inclusion-of-disabling/10179/5 "2007-03-13T16:46:42Z")

</div>

You’re welcome 🙂
