AAF: find_by_contents on AR Association Total Hits

I seem to be getting some behaviour thats unexpected (for me anyway)
when using find_by_contents on an ActiveRecord has_many association. The
results that are returned are only the records that belong to the model
returned, but the total_hits that are being returned appear to be for
the whole table.

e.g.

class Book < AR::Base
has_many :pages
end

class Page < AR::Base
belongs_to :book
acts_as_ferret :fields => [:content]
end

b1 = Book.create(:title => “Book One”)
b1.page.add(:content => “The cat sat on the mat.”)
b1.page.add(:content => “The cat went for a walk in the country.”)

b2 = Book.create(:title => “Book Two”)
b2.page.add(:content => “Once upon a time in a country quite far away.”)
b2.page.add(:content => “There lived a man with his cat.”)

results = b1.pages.find_by_content("cat)
results.total_hits #=> 3
results.results.length #=> 2

b1.pages.total_hits #=> 3

Is this the expected behviour? Is there any way to get total_hits to
report the total_hits only for the pages belonging to b1?

It will make pagination very difficult otherwise.

Cheers,
Steve

On Tue, Aug 28, 2007 at 11:55:21AM +0200, Steve T. wrote:

Is nobody else coming across this problem?

sorry, seems your mail slipped through somehow.

In fact what you are doing is interesting - I never thought of AAF being
used on a has_many collection yet. Looks like in one place the scoping
is working correctly, but when counting the results it doesn’t. As a
work around, use

Page.find_by_contents(query, {}, { :conditions => [“book_id=?”, book.id
] })

to restrict aaf on a special book.

Could you please file a ticket for the enhancement to support calling
aaf directly on collections at
http://projects.jkraemer.net/acts_as_ferret/ ?

cheers,
Jens


Jens Krämer
http://www.jkraemer.net/ - Blog
http://www.omdb.org/ - The new free film database

Is nobody else coming across this problem?

Steve T. wrote:

I seem to be getting some behaviour thats unexpected (for me anyway)
when using find_by_contents on an ActiveRecord has_many association. The
results that are returned are only the records that belong to the model
returned, but the total_hits that are being returned appear to be for
the whole table.

Jens K. wrote:

sorry, seems your mail slipped through somehow.

Thanks for the quick reply, in hindsight it could probably have a better
subject!

Page.find_by_contents(query, {}, { :conditions => [“book_id=?”, book.id ] })

Thanks that works great!

Could you please file a ticket for the enhancement to support calling
aaf directly on collections at
http://projects.jkraemer.net/acts_as_ferret/ ?

http://projects.jkraemer.net/acts_as_ferret/ticket/167

Great plugin, many thanks
Steve