RailsGrunt

a noob working the ruby railroad.

indexing a grandparent field using acts as ferret to search with conditions.

February 10th, 2007

With the help of the helpful folks over on http://www.ruby-forum.com/forum/5 forum, the grunt learned how to search for data in a table if a certain grandparent field matches. in the previous post, i clipped a picture from a site that i developed of the searchbox. basically, the search field has a category parameter that i use to select the particular category a user wants to see. the main category has many sub categories which has many ‘whatever you want here’. here is what the grunt did.

.

in the model, i indexed the grandparent main_category.

acts_as_ferret :fields =>[“title”, “howto”, :main_category]

def main_category

subcategory.maincategory.name

end

in the view:
<= start_form_tag(:action=>‘search’)>

<= text_field_tag(:search)>

<= select(:searchbox, :category, %w{technology food music photography music })>

<= submit_tag “Search”>

<= end_form_tag>
in a controller or wherever you would like to store the search function
def search

@viewObj = paginate_collection Posts.find_by_contents(”#{params[:search].to_s } +main_category: #{params:searchbox}”)

end

now @viewObj will now contain all the Posts where its grandparent (main category) is selected.

acts as ferret, what is it? its a plugin that helps the grunt search.

February 10th, 2007

one of the first ruby on rails plugin that the grunt used was acts_as_ferret. acts as ferret is an awesome plugin that makes searching through tables gruntastically easy. Information about the plugin can be found at http://projects.jkraemer.net/acts_as_ferret/ . Installation is a breeze. I believe I used the latest software version.