Forum: Ruby on Rails Test Failing.....Why?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steven (Guest)
on 2005-11-20 17:37
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
christopher.k.hall (Guest)
on 2005-11-20 21:02
(Received via mailing list)
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/As...
Steven (Guest)
on 2005-11-21 01:12
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/As...
christopher.k.hall (Guest)
on 2005-11-21 02:49
(Received via mailing list)
those methods are overridden...ActiveRecord has replaced them with its
own
implementations.
ezra (Guest)
on 2005-11-21 04:45
(Received via mailing list)
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
Steven <> (Guest)
on 2005-11-21 05:39
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
Steven <> (Guest)
on 2005-11-21 05:40
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
This topic is locked and can not be replied to.