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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:59:48+00:00 2026-06-12T05:59:48+00:00

When I assign a database find to an instance variable in Rails, why do

  • 0

When I assign a database find to an instance variable in Rails, why do future requests to that variable also hit the database? Can this be avoided?

For example, I have 3 models: User, Resource, Opinion, with has_many :through on Opinion

@opinions = current_user.opinions # pulls in all of the user's opinions, which include respective resource ids

1. Calling for resource_id directly does not hit the database:

@opinions.each do |opinion|
  opinion.resource_id  # does not hit the database (as expected)
end

2. Performing a query does hit the database (even though variable has been assigned):

@opinions.find_by_resource_id(1) # DOES hit the database

Why does #2 hit the database? Is there a way to perform the same find without hitting the database?

The information is already contained in the @opinions variable, so a db call does not seem necessary.

  • 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-12T05:59:49+00:00Added an answer on June 12, 2026 at 5:59 am

    If you don’t need anything else in the @opinions array, I would scope your original query to only include opinions with that resource_id

    @opinions = current_user.opinions.where("resource_id = ?", resource_id)
    

    If you already have @opinions and just want to create a new array of objects that match for a specific key/value:

    @opinions_with_resource_id = @opinions.select { |opinion| opinion.resource_id == 1234 }
    

    Check out this other answer for another explanation or if you want to split the answer into multiple arrays.

    Thoughts

    Comment on your last piece of code

    Methods like you called find_by_* are dynamic finders that use method_missing to hit the database and look inside of the column specified by the *.

    Remaining comments from previous answer

    If this object will ever need to access data on the Resource model, don’t forget about the #includes() method, which will keep you from having to run additional queries down the road.

    @opinions = current_user.opinions.includes(:resources)
    

    See http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

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

Sidebar

Related Questions

How do i assign variable cats so that it pulls the rows in database
I've read an blog post(at this moment I can't find the link) where the
I'm sure this is a common problem, but I just can't seem to find
essentially I have a category that you can add comments to, this category shows
This is my first attempt at writing a program that accesses a database from
I would like to assign a variable instead of the string in my following
how can I assign a value to the 'extraParams' property of the following plugin
Is it possible to assign to a list slice in one go, that would
I'm trying to find the way to face this situation. Having these tables in
I pull data from a table in a database and assign it as a

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.