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.

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.

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.

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.

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.