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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:05:19+00:00 2026-06-10T23:05:19+00:00

I have been reading Rails 3 Way and was checking the behavior of CollectionAssociation.include?

  • 0

I have been reading Rails 3 Way and was checking the behavior of CollectionAssociation.include?

As per description in the book:

Checks to see if the supplied record exists in the association
collection and that it still exists in the underlying database
table.

I make couple of tests to check if DB is checked but looks like it is not checking.

> book = Book.first
=> #<Book id: 1, title: "Programming Ruby"...
> last_chapter = book.chapters.last
=> #<Chapter id: 2, title: "Chapter 2", book_id: 1, 
> b.chapters.include?(last_chapter)
=> true 
> last_chapter.destroy
  DELETE FROM "chapters" WHERE "chapters"."id" = ?  [["id", 2]]
=> #<Chapter id: 2, title: "Chapter 2", book_id: 1, 
> b.chapters.include?(last_chapter)
=> true         
> book.chapters.reload
=> [#<Chapter id: 1, title: "Chapter 1", book_id: 1,...]
> b.chapters.include?(last_chapter)
=> false 

I checked source code and found a line load_target if options[:finder_sql] which seems loads the record conditionally.

Do you have any idea when DB will get accessed to check if record is exists in DB?

  • 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-06-10T23:05:21+00:00Added an answer on June 10, 2026 at 11:05 pm

    Rails uses both lazy loading and memoization.

    This means that b.chapters does not load any chapters until you first call the getter.

    Afterwards, destroying the chapter does not change the include? test, because of memoization. Rails does not reload what chapters are in the book. Thus, it will not know that the chapter no longer exists in the book.

    Note that include? (defined on Enumerable/Array objects, but not implemented directly on the ActiveRelation object) will automatically cause Rails to cast the ActiveRelation object book.chapters to an array. Thus, include? will first get the array of chapters, then check if the chapter is included in the array.

    Calling reload explicitly (on any ActiveRecord object) forces rails to reload that particular object, so that the caching of the chapters in the book is thrown away, and the actual chapters are reloaded.

    • You cannot turn off memoization, except by calling reload everywhere you need it to reload.
    • You can tell Rails to eager-load instead of lazy-load, by using includes, eg. book = Book.includes(:chapters).first. This means that Rails will load all the chapters at the same time as it loads the book(s).

    To delete a chapter, while ensuring that book.chapters is up-to-date, you can call books.chapters.delete(book.chapters.last) or books.chapter_ids.delete(book.chapter_ids.last). Using this method, Rails will not need to query the database again to know it is no longer in the collection.

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

Sidebar

Related Questions

I have been developing a sample application after reading Rails 3 Tutorial book. In
Duplicate: Django or Ruby-On-Rails? I have been reading on Ruby on Rails, and it
I am very new to rails, and from what i have been reading and
hello I have been reading about how to make graphs using google-charts-on-rails, I already
I have been reading the book SQL Antipatterns: Avoiding the Pitfalls of Database Programming
I just started looking into Ruby on Rails and have been reading Dave Thomas's
I've been reading about routing in Rails 3 and have been unsuccessful in achieving
I've been reading the book Enterprise Rails by Dan Chak and it got me
I have been reading a bit on JSON and rails (I am new to
Lately I have been reading about the Rack architecture in Passenger/Rails, and how it

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.