Test Failing.....Why?


#1

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


#2

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


#3

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


#4

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


#5

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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#6

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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#7

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