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 3498902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:32:46+00:00 2026-05-18T12:32:46+00:00

I have 2 models with a many-to-many relationship (simplified here with books-authors example): class

  • 0

I have 2 models with a many-to-many relationship (simplified here with books-authors example):

class Book < ActiveRecord::Base  
    has_and_belongs_to_many :authors
end

class Author < ActiveRecord::Base
    has_and_belongs_to_many :books
end

I need to display a list of books, and under each one a list of its authors.
Users are able to filter via a certain author.

I’m trying to filter via an author in the following way:
Let’s assume there’s only one resulting book, and it has 3 authors.

books = Book.find(:all, :include => :authors, :conditions => "authors.id = 5")

When I try to list the different authors of the books retrieved, I get only the author with ID=5.

books.first.authors.size   # => 1
books.first.authors.first.id   # => 5

I would expect rails to go and retrieve all the different authors associated with the books, either during the find query or as a follow-up query, yet it does not.

How can I solve this problem?
Is there something wrong with the associations? the find query?

Thanks.

  • 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-18T12:32:47+00:00Added an answer on May 18, 2026 at 12:32 pm

    This happens because you use :include in your find method. When you do that, you tell Rails to cache the associated result (eager loading) to avoid multiple database queries. And since the conditions you have specified only includes one of the three authors, that is the only one that will be cached and then looped through when you call .authors

    I think the easiest solution would be to change :include to :joins. It will perform the same database query, but it will not cache the associated authors. So when you call authors for each book, it will perform a new database call to retrieve all authors for that book. As I said, the easiest but perhaps not the best since it could potentially result in a lot of queries to the database.

    Here is another way you could do it:

    book_ids = Book.all(:joins => :authors, 
                        :conditions => ["authors.id = ?", 5]).map(&:id)
    books = Book.all(:include => :authors, 
                     :conditions => ["books.id IN (?)", book_ids])
    

    This will first collect all id’s of the books related to a specific author. Then it will query the database again with the resulting book_ids and this time include all authors related to those books and cache it to avoid unnecessary db calls.

    Edit:

    If you really want to use only one call, then I think the only options is to write a little more SQL in the call. Perhaps like this:

    books = Book.all(:include => :authors, 
                     :conditions => ["books.id IN (SELECT book_id FROM authors_books WHERE author_id = ?)", 5])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
Say I have two models, Book and Author with a has_and_belongs_to_many relationship between them.
I have a few model classes with basic one-to-many relationships. For example, a book
Got a question. Say I have two models in a many-to-many relationship (Article, Publication).
I have two models, Item and ShopSection. They have a many-to-many relationship. @Entity(name =
So I have typical 1:M relationship: Car can have many Models I want to
I have an existing app that has many, many models. I'd like to log
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass

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.