Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:49:59+00:00 2026-05-26T20:49:59+00:00

I have a Fieldnote model in my app, which has_many :activities attached to it

  • 0

I have a Fieldnote model in my app, which has_many :activities attached to it through a table called :fieldnote_activities. I then define a searchable index this way:

searchable :auto_index => true, :auto_remove => true do
  integer :id
  integer :user_id, :references => User

  integer :activity_ids, :multiple => true do
    activities.map(&:id)
  end

  text :observations
 end

And then I have a Search model to store / update searches. The search model thus also has its own associations with activities. I then perform my searches like this:

@search = Search.find(params[:id])
@query  = Fieldnote.search do |query|
  query.keywords  @search.terms

  if @search.activities.map(&:id).empty? == false
    query.with    :activity_ids, @search.activities.map(&:id)
  end

end
@fieldnotes = @query.results

Now this all works GREAT. The problem is that if I change which activities that are associated with a fieldnote, the search results do not change because it appears the indices for that fieldnote do not change. I was under the impression that the :auto_index => true and :auto_remove => true flags when I define the searchable index would keep track of new associations (or deleted associations), but this appears not to be the case. How do I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:50:00+00:00Added an answer on May 26, 2026 at 8:50 pm

    You’re right that :auto_index and :auto_remove don’t apply to associated objects, just the searchable object they are specified on.

    When denormalizing, you should use after_save hooks on the associated objects to trigger a reindex where necessary. In this case, you want changes to the Activity model and the FieldnoteActivity join model to trigger a reindex of their associated Fieldnote objects when saved or destroyed.

    class Fieldnote
      has_many :fieldnote_activities
      has_many :activities, :through => :fieldnote_activities
    
      searchable do
        # index denormalized data from activities
      end
    end
    
    class FieldnoteActivity
      has_many :fieldnotes
      has_many :activities
    
      after_save :reindex_fieldnotes
      before_destroy :reindex_fieldnotes
    
      def reindex_fieldnotes
        Sunspot.index(fieldnotes)
      end
    end
    
    class Activity
      has_many :fieldnote_activities
      has_many :fieldnotes, :through => :fieldnote_activities
    
      after_save :reindex_fieldnotes
      before_destroy :reindex_fieldnotes
    
      def reindex_fieldnotes
        Sunspot.index(fieldnotes)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP.NET MVC app, I have an interface which acts as the template
In my model I have a field which should contain only the values 'A',
In my app, users have one template record (in Template table) that sets defaults
Have just started using Visual Studio Professional's built-in unit testing features, which as I
We have script/generate migration add_fieldname_to_tablename fieldname:datatype syntax for adding new columns to a model.
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a simple URLField in my model link = models.URLField(verify_exists = False, max_length
I have a few simple entities that are stored in a simple code table
I have a table in a MySQL table with a fieldname 'product', and want
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.