Forum: Ruby on Rails Struct search

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.
6f4989f18fb2315d4b1db15cccb29ecf?d=identicon&s=25 Mick (Guest)
on 2007-04-21 11: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?

Cheers

Mick
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-04-21 21: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.

b
77d5386c39c011d59cc13808f8a5156b?d=identicon&s=25 unknown (Guest)
on 2007-04-22 18: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.

Regards,

Rein
This topic is locked and can not be replied to.