# Calling validates\_inclusion\_of out of default namespace

**URL:** <https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758>\
**Category:** Rails\
**Created:** [April 8, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758 "2006-04-08T20:44:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [April 8, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758/1 "2006-04-08T20:44:53Z")

</div>

Hi,

I’m trying to run the “validates\_inclusion\_of” method in a before\_save  
hook,  
because the range is dependant on the related data. But I can’t figure  
out how  
to call it. When calling it normally, it says it can’t find it.

I’ve tried several combinations like  
“ActiveRecord::Validations.validates\_inclusion\_of”, but I can’t seem to  
find  
the logic in it. So, how do I call it?

This question could be generalised, because I never know what to call  
when  
something is not in de default namespace. For example, how do I know  
that  
“ActiveRecord::ConnectionAdapters::SQLServerAdapter.select\_one” can be  
run by  
“ActiveRecord::Base.connection.select\_one”? I happen to know this one,  
but I  
would have never guessed…

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [April 8, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758/2 "2006-04-08T21:41:56Z")

</div>

Wiebe C. wrote:

> Hi,
> 
> I’m trying to run the “validates\_inclusion\_of” method in a before\_save  
> hook,  
> because the range is dependant on the related data. But I can’t figure  
> out how  
> to call it. When calling it normally, it says it can’t find it.

This may or may not answer your question, but you can add an “:on =\>  
:save” option to the validates\_inclusion\_of method.

If I understand it correctly, the actual “validates\_inclusion\_of” method  
sets up filters, it’s not an actual procedure. It’s more like a  
configuration option than a method you’d want to run from inside another  
method.

Jeff

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [April 8, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758/3 "2006-04-08T23:36:00Z")

</div>

On Saturday 08 April 2006 21:41, Jeff C.man wrote:

> This may or may not answer your question, but you can add an “:on =\>  
> :save” option to the validates\_inclusion\_of method.
> 
> If I understand it correctly, the actual “validates\_inclusion\_of” method  
> sets up filters, it’s not an actual procedure. It’s more like a  
> configuration option than a method you’d want to run from inside another  
> method.

You understand it correctly.

But, the validates\_inclusion\_of method is run when the object is  
created. When  
you do, all the attributes and relations don’t exist yet. But I do need  
to  
access it’s attributes and relations to acquire the boundries I have to  
give  
to the validates\_inclusion\_of method. Therefore, that method must be run  
_after_ the attributes and relations have been added to the object.

For example, if max\_score is an attribute of the object, saying

validates\_inclusion\_of(:score, :in =\> 0…max\_score)

will say that max\_score is unkown.

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [April 11, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758/4 "2006-04-11T12:49:10Z")

</div>

On 4/8/06, Wiebe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You understand it correctly.
> 
> validates\_inclusion\_of(:score, :in =\> 0…max\_score)
> 
> will say that max\_score is unkown.

I’ve looked at your code and you tried to define the validation from a  
before\_save hook, which is fine if you do it correctly 😉

However, you call validates\_inclusion\_of as an instance method instead  
of a  
class function. If you replace your call to validates\_inclusion\_of from  
the  
before\_save hook method with self.class.validates\_inclusion\_of, your  
problem  
is solved.

- Rowan

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [April 11, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/calling-validates-inclusion-of-out-of-default-namespace/93758/5 "2006-04-11T18:05:47Z")

</div>

On Tuesday 11 April 2006 12:47, BigSmoke wrote:

> I’ve looked at your code and you tried to define the validation from a  
> before\_save hook, which is fine if you do it correctly 😉
> 
> However, you call validates\_inclusion\_of as an instance method instead of a  
> class function. If you replace your call to validates\_inclusion\_of from the  
> before\_save hook method with self.class.validates\_inclusion\_of, your problem  
> is solved.

At first glance, it seemed to work, but calling the validates method on  
the  
class itself has the unfortunate side effect that it starts acting like  
Singletons (as we discussed in private, ie, not here on the list),  
meaning  
that all objects get the validations of the last call to  
class.validates\_inclusion\_of.

I’m getting confused here. The belongs\_to\* and validates\_\* and the likes  
are  
class methods. But when calling one of them in any ActiveRecord::Base  
object,  
aren’t they called when the Base object has already been instantiated?  
If so,  
then why can’t I call them in methods of my models?
