views:

69

answers:

1

I have 2 models A and B.

class A < ActiveRecord::Base
  has_one :b

acts_as_ferret :fields => [:title,:description]

In a_cotroller, i wrote:

@search=A.find_with_ferret(params[:st][:text_search],:limit => :all).paginate :per_page =>10, :page=>params[:page]

The above title and description search is properly working.

class B < ActiveRecord::Base belongs_to :a

Now,I want to perform a text search by using 3 fields; title, description(part of A) and comment(part of B). Where I want to include the comment field to perform the ferret search.Then,what other changes needed.

A: 

The documentation of find_with_ferret indicates that you simply code :store_class_name => :true to enable search over multiple models. While this is true there is a little more to it. To search multiple do the following:

@search = A.find_with_ferret(
  params[:st][:text_search],
  :limit => :all,
  :multi => [B]
).paginate :per_page =>10, :page=>params[:page]

Notice the multi option. This is an array of the additional indexes to search.

Now to get his to work you have to rebuild your indexes after adding :store_class_name => :true to the index definitions.

class A < ActiveRecord::Base
  has_one :b

  acts_as_ferret :store_class_name => :true, :fields => [:title, :description]
end

OR...

You can simply include Bs fields in the index definition:

class A < ActiveRecord::Base
  has_one :b

  acts_as_ferret :fields => [:title, :description],
                 :additional_fields => [:b_content, :b_title]

  def b_content
    b.content
  end

  def b_title
    b.title
  end
end

This makes everything simple, but doesn't allow to search the B model independently of A.

Tony Fontenot
class A < ActiveRecord::Base has_one :b acts_as_ferret :fields => [:title, :description], :additional_fields => [:b_content, :b_title] def b_content b.content end def b_title b.title endendIt is now working...thanks.
jissy