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.
in the view:acts_as_ferret :fields =>[“title”, “howto”, :main_category]
def main_category
subcategory.maincategory.name
end
<= start_form_tag(:action=>‘search’)>in a controller or wherever you would like to store the search function<= text_field_tag(:search)>
<= select(:searchbox, :category, %w{technology food music photography music })>
<= submit_tag “Search”>
<= end_form_tag>
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.
