Hi!

please see comments below.

On Tue, Nov 28, 2006 at 04:18:44PM +0100, Matthew Planchant wrote:
> 
> > Looks as though attributes from many to many relationships are not being 
> > added to the index when a record is amended.
> 
> I have the following relationship between documents and topics:
> 
> 
> 
> class Document < ActiveRecord::Base
>   acts_as_ferret :additional_fields => [:topic_titles]
> 
>   has_many :document_topics, :dependent => true
>   has_many :topics, :through => :document_topics
> 
>   def topic_titles
>     topics.collect { |topic| topic.title }.join ' '
>   end
> end
> 
> class Topic < ActiveRecord::Base
>   has_many :document_topics, :dependent => true
>   has_many :documents, :through => :document_topics
> end
> 
> 
> The update action in documents_controller.rb looks like this:
> 
>   def update
>     params[:document][:topic_ids] ||= []
>     @document = Document.find(params[:id])
>     @topics = (params[:topics] or []).collect { |item| item.to_i }
>     @document.attributes = params[:document]
>     @document.topic_ids = @topics

actually I wonder if this works at all - I'd be surprised if you can
assign ids to a has_many :through relationship like that. However 
you could try this:

      @document.disable_ferret
      if @document.update_attributes(params[:document])
        @document.ferret_update
        flash[:notice] = 'Document was successfully updated.'
        redirect_to :action => 'show', :id => @document
      else
        render :action => 'edit'
      end
    end
 
> I suspect there is something here which means the document has no topics 
> when acts_as_ferret attempts to add the topic_titles to the index.

the problem with indexing data from related objects seems to be that the
after_update hook of aaf is called too early, that is, before all 
relationships are saved. Most often it helps to first save the record
(and skip the indexing, which saves some time) and index the record
after that.

cheers,
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
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to