Acts_as_ferret and associations

Hi all and thank you for the information about associations and AAF. I
have a problem yet with the update of the index cause when a model is
updated, AAF does not updte the upper model using it. Is there a whay to
force the reindexing of a model?

Jens K. wrote:

On Tue, Sep 18, 2007 at 06:10:38AM +0200, Adam J. wrote:

I think i answered my own question.
in the create method for my note i added
Contact.find(params[:id]).ferret_create()

right, however I’d stick that code in a NotesObserver…

Jens

Posted via http://www.ruby-forum.com/.


Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk


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

Matthew Planchant wrote:

Matthew Planchant wrote:

class Book < ActiveRecord::Base
acts_as_ferret :additional_fields => [:author_first_name,
:author_surname]
belongs_to :author

def author_first_name
return author.first_name
end

def author_surname
return author.surname
end
end

This works. Thanks.

hi everyone!

i was following this thread and tried the above, as its exactly my
problem as well:

class User < ActiveRecord::Base

acts_as_ferret :fields => [ :name, :lastname, :firstname,
:focus, :degree, :location,
:classof, :company,
:company_desc, :website, :cellphone, :about ]

acts_as_ferret :additional_fields => [:resume_company_name,
:resume_kind, :resume_description ]

############

def resume_company_name
return resume.company_name
end

def resume_kind
return resume.kind
end

def resume_description
return resume.description
end

############

has_many :resumes

but when building the index, ferret always tells me the following:


Error retrieving value for field resume_company_name: undefined local
variable or method `resume’ for #User:0x26e137c

any ideas?

Donegarden,
I believe your question is like my last one.
All you have to do is find the item in the model and execute the
ferret_create() method on it.
This is the same as an update.

Ie/

Widget.find(params[:id]).ferret_create()

As Jens suggested this would be best in an observer but it will also
work in the controller after you have updated the model

Adam

Donegarden Dg wrote:

Hi all and thank you for the information about associations and AAF. I
have a problem yet with the update of the index cause when a model is
updated, AAF does not updte the upper model using it. Is there a whay to
force the reindexing of a model?

Brendon M. wrote:

I just quickly looked up the inflections to see if that was the problem.
However your relationship is a has_many which therefore means, how does
ferret know which one of the resumes you’re talking about?

The original point of this thread was to do with models that had a 1-1
polymorphic relationship so that you knew where to find the rest of the
data. What you could do is index the resume’s table which would also
include their user_id by default, then do a conditional search on that
user_id if you wanted to search amongst resume’s. You can also add extra
fields like user.name from the resume model because that’s a 1-1
relationship from the perspective of the single resume.

Hope that helps :slight_smile:

Cheers,

Brendon

dude thanks! solved the whole “problem” after a few hours and couldnt
resist to blog about it:

http://blog.foosion.org/2008/01/28/acts_as_ferret-on-associations/

it’s just exactly what you figured out!

greets Nils

Followed this thread and have a has_many_through association indexing
fine. The only problem is the N+1 sql queries incurred with an index
rebuild, full or partial.

Is there any way to enable eager loading on batch index updates?

Cheers,
Sam

I just quickly looked up the inflections to see if that was the problem.
However your relationship is a has_many which therefore means, how does
ferret know which one of the resumes you’re talking about?

The original point of this thread was to do with models that had a 1-1
polymorphic relationship so that you knew where to find the rest of the
data. What you could do is index the resume’s table which would also
include their user_id by default, then do a conditional search on that
user_id if you wanted to search amongst resume’s. You can also add extra
fields like user.name from the resume model because that’s a 1-1
relationship from the perspective of the single resume.

Hope that helps :slight_smile:

Cheers,

Brendon

hi everyone!

i was following this thread and tried the above, as its exactly my
problem as well:

class User < ActiveRecord::Base

acts_as_ferret :fields => [ :name, :lastname, :firstname,
:focus, :degree, :location,
:classof, :company,
:company_desc, :website, :cellphone, :about ]

acts_as_ferret :additional_fields => [:resume_company_name,
:resume_kind, :resume_description ]

############

def resume_company_name
return resume.company_name
end

def resume_kind
return resume.kind
end

def resume_description
return resume.description
end

############

has_many :resumes

but when building the index, ferret always tells me the following:


Error retrieving value for field resume_company_name: undefined local
variable or method `resume’ for #User:0x26e137c

any ideas?

Hi!

On Thu, Mar 13, 2008 at 05:01:26AM +0100, Sam G. wrote:

Followed this thread and have a has_many_through association indexing
fine. The only problem is the N+1 sql queries incurred with an index
rebuild, full or partial.

Is there any way to enable eager loading on batch index updates?

Yes. Acts_as_ferret defines a class method named records_for_rebuild on
your model, which you may override to use a customized find call.

See lib/class_methods.rb for the original implementation.

Cheers,
Jens


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