Struct search

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?



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.


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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs