# Rake test and validates\_inclusion\_of

**URL:** <https://www.ruby-forum.com/t/rake-test-and-validates-inclusion-of/82189>\
**Category:** Rails\
**Created:** [January 17, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/rake-test-and-validates-inclusion-of/82189 "2007-01-17T22:31:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 17, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/rake-test-and-validates-inclusion-of/82189/1 "2007-01-17T22:31:43Z")

</div>

Hello,

I am just starting off with Rails, so I apologize in advance if there  
is a terribly obvious answer to my problem. I’ve done some searching  
and found similar problems to my own, but nothing which yielded a  
solution.

I have a handful of models that I am unit testing. The tests for each  
unit run successfully, but when I do a rake test, many of the tests  
fail.

The assertions that fail are always ones that are asserting that a  
model, which has a reference to another model, is OK. So for example,  
one assertion that passes when I do `ruby test/unit/department_test.rb`, but fails when I do `rake test` is:

department = Department.new(:name =\> “super department”,  
:parent\_id =\> departments(:administration\_department).id)  
assert department.save

When I print out department.errors, I see that parent\_id is invalid  
because it “is not included in the list”.

The validation I have on parent\_id is:

validates\_inclusion\_of :parent\_id, :in =\> Department.find\_all.collect {  
|e| e.id }

So, maybe the department table wasn’t the best example because it’s  
acts\_as\_tree, BUT all the failed assertions are the same:

- they’re failing on asserting that an object is good
- the object has a reference to another model
- that reference is being validated with validates\_inclusion\_of
- that reference never passes validation
- they pass when i run the test script individually

So my hypothesis is that the fixture data isn’t being loaded in before  
the constraints for the reference\_id are being defined, but ONLY in the  
rake test environment.

Does that make sense? Am I missing something obvious? Any thoughts  
would be appreciated.

I’m using Rails 1.1.6 and MySQL (with no foreign key constrains in the  
DB).

Thanks,  
Julia

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 19, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/rake-test-and-validates-inclusion-of/82189/2 "2007-01-19T23:52:21Z")

</div>

Well, I got no responses to this post explaining why this happens, but  
I did work around this issue.

For future reference, in case anyone else has this problem, I fixed the  
rake test errors by validating the reference ID in the validate  
function instead of using validates\_inclusion\_of.

So, I removed this:

validates\_inclusion\_of :parent\_id, :in =\> Department.find\_all.collect {  
|e| e.id }

… and replaced it with this:

protected  
def validate  
unless Department.exists?(parent\_id)  
errors.add(:parent\_id, “is not a valid department”)  
end  
end

Julia

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/rake-test-and-validates-inclusion-of/82189/3 "2007-03-15T21:35:33Z")

</div>

I was having the same problem just the other day, this solution worked  
but I ended up using  
validates\_associated.

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000948](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000948)
