To edit pages or tickets please login with username/password: aaf/aaf

Ticket #167 (closed defect: fixed)

Opened 1 year ago

Last modified 11 months ago

find_by_contents on HasMany association scopes total_hits to whole table

Reported by: aaf Assigned to: jk
Priority: minor Milestone: 0.4.2
Component: 0plugin Version:
Keywords: Cc: steve.tooke@gmail.com

Description

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

JK suggested following workaround on Ruby Forum:

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

Change History

08/29/07 06:51:18 changed by jk

  • status changed from new to assigned.
  • milestone set to 0.4.2.

09/05/07 15:19:09 changed by aaf

  • cc set to steve.tooke@gmail.com.

I've discovered a problem with the workaround. Using conditions like above gives incorrect total hits results when :offset is set. e.g.

results = Page.find_by_contents(query, {:limit => 10, :offset => 0}, { :conditions => ["book_id=?", book.id] })
results.total_hits #=> 45

results = Page.find_by_contents(query, {:limit => 10, :offset => 10}, { :conditions => ["book_id=?", book.id] })
results.total_hits #=> 35

results = Page.find_by_contents(query, {:limit => 10, :offset => 20}, { :conditions => ["book_id=?", book.id] })
results.total_hits #=> 25

This may need reporting as a separate defect.

11/17/07 16:10:29 changed by jk

  • status changed from assigned to closed.
  • resolution set to fixed.

Fixed (both issues).

To edit pages or tickets please login with username/password: aaf/aaf