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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:03:58+00:00 2026-05-22T16:03:58+00:00

Is Rails’ find(x) method on a model lazy? If not, what is the equivalent?

  • 0

Is Rails’ find(x) method on a model lazy? If not, what is the equivalent?

I am new to Rails, so I found myself writing scopes like this:

class Course < ActiveRecord::Base
  scope :by_instructor_id, lambda { |instructor_id| where(:instructor_id => instructor_id) }
  scope :by_course_template_id, lambda { |course_template_id| where(:course_template_id => course_template_id ) }
  scope :by_company_id, lambda { |company_id| joins(:instructor).merge(CompanyUser.by_company_id(company_id)) }
end

It’s not a lot of work, but now I’m asking myself… if Rails provided these with a scope, I wouldn’t have to write them.

So, does Rails offer them? Can I do something like the below code and only make it do 1 database call?

Company.find(params[:id]).instructors.courses

instead of

Course.by_company_id(params[:id])

Which is correct? I know Course.by_company_id(params[:id]) is only 1 database call. It is very familiar to writing SQL or queries in Hibernate. But if you can write it the other way, maybe one should?

However, I don’t want to write Company.find(params[:id]).instructors.courses if it results in more than 1 database call. I can see the advantage though because it means never having to write the 3 scopes I showed you above, but I am worried that Company.find(x) is not lazy. Is it?

  • 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-22T16:03:59+00:00Added an answer on May 22, 2026 at 4:03 pm

    Try using #scoped method on a model before calling #find:

    user = User.scoped.instructors.courses.find(params[:id])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

rails 2.3.4, sqlite3 I'm trying this Production.find(:all, :conditions => ["time > ?", start_time.utc], :order
Rails 2.3.11. I have a search form like this... - form_for(@search) do |form| #
Rails uses the concept of migrations to deal with model changes using the ActiveRecord
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect
Rails gurus: I've just discovered named_scope thanks to another SO user. :) I'd like
Rails 3.1, ActiveAdmin 0.3.4. My question is somewhat similar to this one but different
Rails offers helpers like 24.hours , 1.minute etc. How can I get the number
Rails newbie here so just want to be sure I'm doing this right. I
Rails 3.1.1 , For using ajax in my application I used remote=>true method on
Rails 3.2.1 app name: demo database: mongoDB with mongoid I have this scaffolding set

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.