# How to use find method on table with two column index

**URL:** <https://www.ruby-forum.com/t/how-to-use-find-method-on-table-with-two-column-index/185966>\
**Category:** Rails\
**Created:** [March 29, 2010, 8:53pm UTC](https://www.ruby-forum.com/t/how-to-use-find-method-on-table-with-two-column-index/185966 "2010-03-29T20:53:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 29, 2010, 8:53pm UTC](https://www.ruby-forum.com/t/how-to-use-find-method-on-table-with-two-column-index/185966/1 "2010-03-29T20:53:13Z")

</div>

I created an index with person\_id and club\_id but I can’t figure out  
how to properly access it (doesn’t show up in the find\_by… lists)

Can someone provide me a sample find statement to best use this index

Thanks.

class CreateMembers \< ActiveRecord::Migration  
def self.up  
create\_table :members do |t|  
t.integer :club\_id  
t.integer :person\_id  
t.text :comment  
t.string :status  
t.string :member\_type  
t.integer :registrations\_count

```
  t.timestamps
end

add_index :members, :club_id
add_index :members, :person_id
add_index :members, [:person_id, :club_id]

```

end

def self.down  
drop\_table :members  
end  
end

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 29, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/how-to-use-find-method-on-table-with-two-column-index/185966/2 "2010-03-29T22:39:09Z")

</div>

On Mar 29, 7:51 pm, RVRoadie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I created an index with person\_id and club\_id but I can’t figure out  
> how to properly access it (doesn’t show up in the find\_by… lists)
> 
> Can someone provide me a sample find statement to best use this index

You don’t need to do anything explicitly - just find :all, :conditions  
=\> … will use the index if needed (you may have been looking for  
find\_by\_xxx\_and\_yyy though)  
The index on person\_id is usually superfluous - when you have a  
multicolumn index then any left prefix of that index is also an index  
ie the index on [:person\_id, :club\_id] can be used as an index on just  
person\_id

Fred

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 4, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/how-to-use-find-method-on-table-with-two-column-index/185966/3 "2010-04-04T13:13:05Z")

</div>

Hi there

What you are trying to find is (I think it’s the easiest option )

class CreateListSongs \< ActiveRecord::Migration  
def self.up  
create\_table :members, :primary\_key =\>[:club\_id, :person\_id] do |t|  
t.integer :club\_id  
t.integer :person\_id  
t.text :comment  
t.string :status  
t.string :member\_type  
t.integer :registrations\_count  
t.timestamps

```
end

```

end

def self.down  
drop\_table :members  
end  
end

Hope this helps

Best regards

Miquel

On Mon, 29 Mar 2010 11:51:48 -0700 (PDT)  
RVRoadie wrote:

> ```
> t.integer :club_id
> add_index :members, :person_id
> 
> ```
> 
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

Miquel (a.k.a. Ktalà a.k.a. Ton)  
Linux User #286784  
GPG Key : 4D91EF7F  
Debian GNU/Linux (Linux wolverine 2.6.23.1)

Welcome to the jungle, we got fun and games  
Guns n’ Roses
