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

  • SEARCH
  • Home
  • 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 6564503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:58:29+00:00 2026-05-25T13:58:29+00:00

I have a model Author with a has_many relationship to model Book. Books are

  • 0

I have a model Author with a has_many relationship to model Book. Books are indexed by sunspot, and have an indexed field :author_name which pulls the name of the Author associated with it.

I have a form that allows you to create an author and add books at the same time via

accepts_nested_attributes_for :book

My problem is that when creating a new author with books the new books are not indexed correctly. I believe that when the books are created and the after_save sunspot index is called the associated new author has not yet been committed and so its name does not exist.

What’s the best solution for this? I’ve tried adding an after_commit hook on the author model to index all of the associated books, but when that method loops through the books their ids / associations are nil.

  • 1 1 Answer
  • 3 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-25T13:58:31+00:00Added an answer on May 25, 2026 at 1:58 pm

    I had to add an after_save hook on the parent model to look up all of the children and save them. Inside that after_save I had to re-lookup all of the books based on ID – otherwise their associated Author was nil.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a author model class Author include Mongoid::Document field :name end In My
So lets say I have an Author Model Authors has_many Books For all intents
I have created a blog app which its model has an author field like
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I have model Foo which has field bar. The bar field should be unique,
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
Hypothetical scenario: I have two models: Author and Book . The Book model has
I have two models books and authors joined by books_authors model. A book has
Say I have two models, Book and Author with a has_and_belongs_to_many relationship between them.
I have a Person model that has a foreign key relationship to Book ,

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.