# Test Failing.....Why?

**URL:** <https://www.ruby-forum.com/t/test-failing-why/48438>\
**Category:** Rails\
**Created:** [November 20, 2005, 4:37pm UTC](https://www.ruby-forum.com/t/test-failing-why/48438 "2005-11-20T16:37:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 20, 2005, 4:37pm UTC](https://www.ruby-forum.com/t/test-failing-why/48438/1 "2005-11-20T16:37:54Z")

</div>

Hi I’m running the following test in rails and I can’t seem to figure  
out its behaviour. The @institution object has an array of LicenseType  
objects, license\_types() starts of as an array of 2. Basically, I’m  
removing one of the LicenseType objects from the license\_types array,  
saving @institution and then confirming that its size is now 1 instead  
of 2. here’s what I don’t get. If I remove the license type with the  
‘delete\_if’ block, then call save(), there are still two license\_types  
in the Array. However, if I remove the LicenseType with the ‘delete(  
ltype )’ method, then call save() there is 1 LicenseType in the array. I  
would think that both of these methods should have the same result, but  
only the latter is passing the test.

Any ideas?

# failing test

def test\_update()  
assert\_equal(‘The Mathworks, Inc.’, @institution.name())  
@institution.name = ‘Mathworks’  
assert(‘Mathworks’, @institution.name())

assert\_kind\_of(Array, @institution.license\_types())  
assert\_equal(2, @institution.license\_types().size())

ltype = @institution.license\_types().detect() { |lt| lt.name ==  
‘group’ }  
assert\_equal(‘group’, ltype.name())

@institution.license\_types().delete\_if { |lt| lt.name() == ‘group’ }  
assert\_equal(1, @institution.license\_types().size())

assert(@institution.save())  
@institution.reload()

assert\_equal(‘Mathworks’, @institution.name())  
// test fails here as license\_types().size() == 2  
assert\_equal(1, @institution.license\_types().size())

end

# passing test

def test\_update()  
assert\_equal(‘The Mathworks, Inc.’, @institution.name())  
@institution.name = ‘Mathworks’  
assert(‘Mathworks’, @institution.name())

assert\_kind\_of(Array, @institution.license\_types())  
assert\_equal(2, @institution.license\_types().size())

ltype = @institution.license\_types().detect() { |lt| lt.name ==  
‘group’ }  
assert\_equal(‘group’, ltype.name())

@institution.license\_types().delete(ltype)  
assert\_equal(1, @institution.license\_types().size())

assert(@institution.save())  
@institution.reload()

assert\_equal(‘Mathworks’, @institution.name())  
assert\_equal(1, @institution.license\_types().size())

end

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 20, 2005, 8:02pm UTC](https://www.ruby-forum.com/t/test-failing-why/48438/2 "2005-11-20T20:02:54Z")

</div>

delete is an association method, therefore it deletes the associated  
record  
from the database when it is deleted from the collection. delete\_if is  
not  
an association method, therefore the object will only be removed from  
the  
collection, but the record is not deleted from the database.

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 21, 2005, 12:12am UTC](https://www.ruby-forum.com/t/test-failing-why/48438/3 "2005-11-21T00:12:40Z")

</div>

Hi Christopher,

Thanks for your reply! Ok, that makes sense, I foolishly assumed that I  
was calling the delete() method from the Array object.

If you don’t mind, I’d like to ask another question. If the method  
‘license\_types()’ returns an object of Array–which my test  
confirms–and I then call something like ‘license\_types().delete()’ why  
isn’t the delete() method of Array being used?

In fact, the module ENUM which is mixed into class Array has a method  
named ‘find()’ just like ActiveRecord. In my test I had to use the  
alias method for find() in my block…detect()–because the  
ActiveRecord find() kept getting called. How does one ensure that the  
appropriate method gets called?

Thanks,  
Steven

christopher.k.hall wrote:

> delete is an association method, therefore it deletes the associated  
> record  
> from the database when it is deleted from the collection. delete\_if is  
> not  
> an association method, therefore the object will only be removed from  
> the  
> collection, but the record is not deleted from the database.
> 
> [http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 21, 2005, 1:49am UTC](https://www.ruby-forum.com/t/test-failing-why/48438/4 "2005-11-21T01:49:16Z")

</div>

those methods are overridden…ActiveRecord has replaced them with its  
own  
implementations.

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 21, 2005, 3:45am UTC](https://www.ruby-forum.com/t/test-failing-why/48438/5 "2005-11-21T03:45:47Z")

</div>

You can explicitly get the array version of delete by doing this:

> 'license\_types.to\_a.delete

Cheers-

-Ezra

On Nov 20, 2005, at 4:47 PM, Chris H. wrote:

> ## appropriate method gets called? delete\_if is
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 21, 2005, 4:39am UTC](https://www.ruby-forum.com/t/test-failing-why/48438/6 "2005-11-21T04:39:21Z")

</div>

Thanks Ezra and Christopher,

So the container that ActiveRecord uses to store objects on the ‘many’  
side of a relationship, is not a normal Array I take it. Do either of  
you guys know if rails has this info in the API docs…I can’t find  
it. I checked out a copy of rails and I now see that ActiveRecord uses  
the AssociationCollection class.

Thanks Again!  
Steven

ezra wrote:

> You can explicitly get the array version of delete by doing this:
> 
> > 'license\_types.to\_a.delete
> 
> Cheers-
> 
> -Ezra
> 
> On Nov 20, 2005, at 4:47 PM, Chris H. wrote:
> 
> > ## appropriate method gets called? delete\_if is
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> -Ezra Z.  
> WebMaster  
> Yakima Herald-Republic Newspaper  
> removed\_email\_address@domain.invalid  
> 509-577-7732

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [November 21, 2005, 4:40am UTC](https://www.ruby-forum.com/t/test-failing-why/48438/7 "2005-11-21T04:40:20Z")

</div>

Thanks Ezra and Christopher,

So the container that ActiveRecord uses to store objects on the ‘many’  
side of a relationship, is not a normal Array I take it. Do either of  
you guys know if rails has this info in the API docs…I can’t find  
it. I checked out a copy of rails and I now see that ActiveRecord uses  
the AssociationCollection class.

Thanks Again!  
Steven
