# Struct search

**URL:** <https://www.ruby-forum.com/t/struct-search/92601>\
**Category:** Rails\
**Created:** [April 21, 2007, 11:33am UTC](https://www.ruby-forum.com/t/struct-search/92601 "2007-04-21T11:33:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 21, 2007, 11:33am UTC](https://www.ruby-forum.com/t/struct-search/92601/1 "2007-04-21T11:33:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 21, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/struct-search/92601/2 "2007-04-21T21:51:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 22, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/struct-search/92601/3 "2007-04-22T18:57:25Z")

</div>

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
