Forum: Ruby on Rails Struct search

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mick (Guest)
on 2007-04-21 13:33
Hi guys, another question:

I have a struct, which contains records returned from a query. I then
perform a loop which runs another query which adds more records to the
struct, but I would like to check whether the record already exists
before adding it, is there a way to search a struct, say by id, to check
the existance of said record before I add it?


Ben M. (Guest)
on 2007-04-21 23:51
(Received via mailing list)
Put the records into a Hash instead, using the id as the key. Then if
you put in a record with the same id, it just overwrites it.

unknown (Guest)
on 2007-04-22 20:57
(Received via mailing list)
What is the purpose of using a Struct? ActiveRecord objects already
have setter and getter methods.
you can just use a normal array and either uniq it or use Array#| (the
set union operator) like @collection | @new_records to save a bit.

Also note that a Hash is unordered.


This topic is locked and can not be replied to.