# Searching within active record results - ror n00b

**URL:** <https://www.ruby-forum.com/t/searching-within-active-record-results-ror-n00b/72523>\
**Category:** Rails\
**Created:** [February 23, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/searching-within-active-record-results-ror-n00b/72523 "2006-02-23T22:03:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [February 23, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/searching-within-active-record-results-ror-n00b/72523/1 "2006-02-23T22:03:53Z")

</div>

I am trying to set up a search page that looks within a product catalog.

There are several many to many relationships with the items object.

I am trying to be able to narrow searchs and then allow for text  
searches  
using tsearch2 and postgres.

@format = Format.find(@params[:format\_id])  
@items = @format.items

And then do a find\_by\_sql on the @items, but I know this isnt how you do  
this.  
@items.find\_by\_sql("SELECT \* FROM items WHERE idxfti @@  
to\_tsquery(‘default’, ‘#{@str}’)

class Format \< ActiveRecord::Base

```
has_and_belongs_to_many :items

belongs_to :parent,
            :class_name => "Category",
            :foreign_key => "parent_id"

has_many :children,
            :class_name => "Category",
            :foreign_key => "parent_id",
            :order => "title",
            :dependent => true

validates_presence_of :title

```

end

class Item \< ActiveRecord::Base

```
has_and_belongs_to_many :formats
has_and_belongs_to_many :keywords
has_and_belongs_to_many :images

has_many :reviews

```

end

Thanks for any pointers
