Hi Rami,
Thanks for the reply… I am still not able to get it to work though. I
am getting the same error that I was getting during my own attempts
(well at one stage anyway!) although when I try in the console, I get a
different error.
Here is my code…
Code
class Product < ActiveRecord::Base
has_many :Components
acts_as_ferret( :store_class_name => true, :fields => [
:index_components, ‘name’, ‘description’ ] )
def index_components
@index = Array.new
for component in self.components
@index << component.name.to_s
@index << component.description.to_s
end
@index.join(" ")
end
end
class Component < ActiveRecord::Base
belongs_to :Product
acts_as_ferret( :store_class_name => true, :fields => [ 'name',
‘description’ ] )
end
class SearchController < ApplicationController
def search
@query = params[:search] || ''
unless @query.blank?
@results = Product.multi_seach( @query, [ Component ] )
end
....
end
End Code
The error I am getting in the browser is, “undefined method
`multi_seach’ for Product:Class” and the error I am getting the console
if I try it manually is, “TypeError: nil is not a symbol”.
This made me think that I need to include the acts_as_ferret plugin
somewhere in my code, but then why would find_by_contents work? Anyway,
at least you have confirmed that my code is correct in my models and
controller. Any ideas on these errors?
Regards,
Graham
Rami wrote:
Graham,
First, to do multi_search make sure to declare :store_class_name => true
in both the Product and Component files in order to do multi_search.
class Product < ActiveRecord::Base
acts_as_ferret :store_class_name => true
…
end
class Component < ActiveRecord::Base
acts_as_ferret :store_class_name => true
…
end
Next, if you want to retrieve products based on in its components, you
have to index your product based on the appropriate component values:
class Product < ActiveRecord::Base
acts_as_ferret :store_class_name => true,
:fields => [ ‘field1’,
‘field2’,
:index_components
]
…
end
def index_components
@index = Array.new
for component in self.components
@index << component.field1.to_s
@index << component.field2.to_s
end
@index.join(" ")
end
Basically, acts_as_ferret will index any model based on the text your
throw at it. Now, something like Product.find_by_contents(“query term”)
should retrieve products based on text in all of its component fields.
Finally, you can do multi_search via:
@results = Product.multi_search(“query term”, [Component])
This search will retrieve both Product and Component models in the array
@results. Now that you have both, you have to check what the class type
is in your view files before displaying the search results:
<% @results.each do |result| %>
<% if result.class == Product %>
code to display your Product here
<% else %>
code to display your Component here
<% end %>
<% end %>
I wasn’t able to go into all the details here, but I hope this helps.
Rami
Graham wrote:
I have been trying to use multi_search to search accross multiple
associated models, but I have had no luck at all. I have scoured the net
and this forum for all similar posts, but none of them contain enough
code for me to get it to work.
I am successfully able to search individual models, and then display the
results without a problem.
I have the following 2 models, Product and Component. The Product model
has_many Components, and the Component model belongs_to Product.
Could anyone provide an example of how they would search the Product
model for a query term, which will then return not only the Products
that match the query, but the Components (related to the Products) as
well.
I would really appreciate a “back-to-basics” example, as I think I have
confused myself completely in my attempts.
Thanks.