[0.10.0 - acts_as_ferret] Problem while saving new items

Disclaimer: ferret newbie here, don’t blame too hard.

Hi, I’m trying to apply acts_as_ferret to apply search to my Person
model:

class Person < ActiveRecord::Base
validates_presence_of :name, :surname
acts_as_ferret :fields => [ ‘name’, ‘surname’ ]

But when I try to save a new Person instance I get this error:

ferret_create/update: Person : 20
creating doc for class: Person, id: 20
Adding field name with value ‘Paul’ to index
Adding field surname with value ‘Smith’ to index
e[4;36;1mSQL (0.000755)e[0m e[0;1mROLLBACKe[0m

NoMethodError (You have a nil object when you didn’t expect it!
The error occured while evaluating nil.commit):
/usr/local/lib/ruby/gems/1.8/gems/ferret-0.10.0/lib/ferret/index.rb:203:in
<<' /usr/local/lib/ruby/1.8/monitor.rb:229:insynchronize’
/usr/local/lib/ruby/gems/1.8/gems/ferret-0.10.0/lib/ferret/index.rb:186:in
<<' /vendor/plugins/acts_as_ferret/lib/acts_as_ferret.rb:564:inferret_create’

Any idea? I’m working with ferret 0.10.0 and the new acts_as_ferret
version.

Thanks in advance.

Well, It seems to work simply replacing this line (203) on
/ferret-0.10.0/lib/ferret/index.rb:

@writer.commit # original
@writer.commit if @writer # patched

I’m not sure if the problem comes from acts_as_ferret or is a problem on
ferret itself.

It’s strange that I am the only affected (saving ActiveRecord items is a
so-common operation that acts_as_ferret team have surely tested it
before releasing the new version). Can anybody confirm if the patch is
needed? And, if so, is it correct?

Thanks in advance.

On Thu, Aug 24, 2006 at 09:47:04PM +0200, Raul M. wrote:

Well, It seems to work simply replacing this line (203) on
/ferret-0.10.0/lib/ferret/index.rb:

@writer.commit # original
@writer.commit if @writer # patched

I might be wrong but this doesn’t look ok to me, because it prevents
the change the index class intends to make to the index from being
written.

I have a patch (Parked at Loopia) for ferret’s index.rb file
that fixes this issue by adding ensure_writer_open() before the line in
question, which initializes the @writer variable if it’s not already
present.

I’m not sure if the problem comes from acts_as_ferret or is a problem on
ferret itself.

It’s strange that I am the only affected (saving ActiveRecord items is a
so-common operation that acts_as_ferret team have surely tested it
before releasing the new version).

Well, Ferret 0.10 is still new (and acts_as_ferret is only compatible to
it for a day or so) - so you’re probably the first on the list who tried
it out :slight_smile:

I discovered this bug when porting aaf to Ferret 0.10, and made the
patch. I should have communicated that when announcing the 0.10
compatibility, but I forgot…
I think Dave will release a fixed gem soon, so the patch won’t be
necessary for a long time.

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

On Thu, Aug 24, 2006 at 07:34:05PM +0200, Raul M. wrote:

Disclaimer: ferret newbie here, don’t blame too hard.

Hi, I’m trying to apply acts_as_ferret to apply search to my Person
model:

class Person < ActiveRecord::Base
validates_presence_of :name, :surname
acts_as_ferret :fields => [ ‘name’, ‘surname’ ]

As of Ferret 0.10, you should use symbols only as field names.

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

On 8/25/06, Jens K. [email protected] wrote:

On Thu, Aug 24, 2006 at 09:47:04PM +0200, Raul M. wrote:

Well, It seems to work simply replacing this line (203) on
/ferret-0.10.0/lib/ferret/index.rb:

@writer.commit # original
@writer.commit if @writer # patched

I might be wrong but this doesn’t look ok to me, because it prevents
the change the index class intends to make to the index from being
written.

Actually, as the code is now, I think Raul’s patch will also work. I
would expect the error to be happening when :auto_flush is set to true
which would cause @writer to be closed after the delete method has
been called. The commit isn’t necessary if @writer has already been
closed since a commit happens automatically when the writer is closed.
It made me realize that I may as well call @writer’s delete method
directly in the add_document method. Thank you both of you.

Cheers,
Dave

I might be wrong but this doesn’t look ok to me, because it prevents
the change the index class intends to make to the index from being
written.

As a completely ruby/rails/ferret newbie I also thought that my try was
wrong but I had to try :wink:

I have a patch (Parked at Loopia) for ferret’s index.rb file
that fixes this issue by adding ensure_writer_open() before the line in
question, which initializes the @writer variable if it’s not already
present.

Thanks for your patch! I will apply it until the new gem release.

I think Dave will release a fixed gem soon, so the patch won’t be
necessary for a long time.

I hope so :slight_smile:

As of Ferret 0.10, you should use symbols only as field names.

Great tip! Thank you very much Jens!