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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:33:44+00:00 2026-05-28T17:33:44+00:00

Based on the Rails 3 API, the difference between a scope and a class

  • 0

Based on the Rails 3 API, the difference between a scope and a class method is almost non-existent.

class Shipment < ActiveRecord::Base
  def self.unshipped
    where(:shipped => false)
  end
end

is the same as

scope :unshipped, where(:shipped => false)

However, I’m finding that I’m sometimes getting different results using them.

While they both generate the same, correct SQL query, the scope doesn’t always seem to return the correct values when called. It looks like this problem only occurs when its called the same way twice, albeit on a different shipment, in the method. The second time it’s called, when using scope it returns the same thing it did the first time. Whereas if I use the class method it works correctly.

Is there some sort of query caching that occurs when using scope?

Edit:

order.line_items.unshipped

The line above is how the scope is being called. Orders have many line_items.

The generate_multiple_shipments method is being called twice because the test creates an order and generates the shipments to see how many there are. It then makes a change to the order and regenerates the shipments. However, group_by_ship_date returns the same results it did from the first iteration of the order.

def generate_multiple_shipments(order)
  line_items_by_date = group_by_ship_date(order.line_items.unshipped)

  line_items_by_date.keys.sort.map do |date|
    shipment = clone_from_order(order)
    shipment.ship_date = date
    line_items_by_date[date].each { |line_item| shipment.line_items << line_item }
    shipment
  end
end

def group_by_ship_date(line_items)    
  hash = {}
  line_items.each do |line_item|
    hash[line_item.ship_date] ||= []
    hash[line_item.ship_date] << line_item
  end
  hash
end
  • 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-28T17:33:45+00:00Added an answer on May 28, 2026 at 5:33 pm

    I think your invocation is incorrect. You should add so-called query method to execute the scope, such as all, first, last, i.e.:

    order.line_items.unshipped.all
    

    I’ve observed some inconsistencies, especially in rspec, that are avoided by adding the query method.

    You didn’t post your test code, so it’s hard to say precisely, but my exeprience has been that after you modify associated records, you have to force a reload, as the query cache isn’t always smart enough to detect a change. By passing true to the association, you can force the association to reload and the query to re-run:

    order.line_items(true).unshipped.all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Rails app I have Sales and Salespeople: class Sale < ActiveRecord::Base belongs_to
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have a parent class in rails that inherits from ActiveRecord::Base. I'm trying to
I have created a simple API for a Rails application using token-based-authentication that supports
An active_resource based class: Contact.search(:email => ['bar@foo.com','foo@bar.com']) would produce this: ?email[]=bar@foo.com&email[]=foo@bar.com The specific API
I have a many-to-many relationship like this: class Event < ActiveRecord::Base has_many :calendar_events has_many
I am using ActiveResource against a non-Rails REST API... in fact even the Rest
I've been working on a Google Maps based Rails app and it was working
I am currently putting together a rails-based web application which will only serve and

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.