Index help on Polymorphic Associations

Hello,

I’m a newbie to aaf and rails and I hope anyone can help me with this.
I have the following Models:

class Project < ActiveRecord::Base
acts_as_authorizable
acts_as_audited :except => [:created_by, :updated_by ],
:user_class_name => ‘AuthenticatedSystem’, :user_method =>
‘current_user’
acts_as_ferret :fields => {:name => {:store => :yes},
:description => {:store => :yes},
:project_notes => {:store => :yes}}

has_many :notes, :as => :notable

def project_notes
@index = Array.new
for note in self.notes
@index << note.details
end
@index.join(" ")
end

class Note < ActiveRecord::Base
acts_as_authorizable
acts_as_audited :except => [:created_by, :updated_by ],
:user_class_name => ‘AuthenticatedSystem’, :user_method =>
‘current_user’
acts_as_ferret :fields => {:details => {:store => :yes},
:notable_id => {},
:notable_type => {}}

belongs_to :notable, :polymorphic => true

But when i searched for a project with the note ‘test’:

@results = Project.find_by_contents(‘test’)

it returns 0 results. I checked in the logs and it created the index:

Processing NotesController#create (for 127.0.0.1 at 2006-12-11 13:53:53)
[POST]

creating doc for class: Note, id: 17
Adding field notable_id with value ‘9’ to index
Adding field details with value ‘test project 9’ to index
Adding field notable_type with value ‘Project’ to index

I also added this to the NotesController:

def create
@note = Note.new(params[:note])
@note.disable_ferret(:index_when_finished) do
@successful = @note.save
end


end

This doesn’t seem to work either, but after I have rebuild the index,
the note ‘test’ now appears on the results. Did I miss something here?

Any help is greatly appreciated.

Thanks much,
Jackie

On Mon, Dec 11, 2006 at 07:57:40AM +0100, Jackie wrote:

Hello,

I’m a newbie to aaf and rails and I hope anyone can help me with this.
I have the following Models:

class Project < ActiveRecord::Base
[…]
acts_as_ferret :fields => {:name => {:store => :yes},
:description => {:store => :yes},
:project_notes => {:store => :yes}}

has_many :notes, :as => :notable
[…]
def project_notes
@index = Array.new
for note in self.notes
@index << note.details
end
@index.join(" ")
end
[…]
class Note < ActiveRecord::Base
[…]
acts_as_ferret :fields => {:details => {:store => :yes},
:notable_id => {},
:notable_type => {}}

belongs_to :notable, :polymorphic => true

[…]

creating doc for class: Note, id: 17
Adding field notable_id with value ‘9’ to index
Adding field details with value ‘test project 9’ to index
Adding field notable_type with value ‘Project’ to index

[…]

This doesn’t seem to work either, but after I have rebuild the index,
the note ‘test’ now appears on the results. Did I miss something here?

yeah :wink:

as you can see from the log above, the index is updated correctly when
the note is saved. The problem is which index gets updated. It’s the
notes index in this case. The index you are searching when doing
Project.find_by_contents is the project index.

To solve your problem, you should find the relevant project after
creating the note, and do a ferret_update with it, i.e.:

@note = Note.create params[:note]
@note.project.ferret_update

(this will work only if project_id is contained in params[:note])

if you don’t need to find a single note with ferret (with
Note.find_by_contents), you can skip the acts_as_ferret call in
note.rb.

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