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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:32:24+00:00 2026-05-24T00:32:24+00:00

my model has default_scope(:order => ‘created_at’ ) my tests (rspec, factory girl, shoulda, etc.)

  • 0

my model has default_scope(:order => ‘created_at’ )
my tests (rspec, factory girl, shoulda, etc.)
are:

require 'spec/spec_helper.rb'

describe CatMembership do
  context "is valid" do
    subject { Factory.build(:cat_membership) }
    it { should be_valid }
    it { should belong_to :cat}
    it { should belong_to :cat_group}
    it { should have_db_column(:start_date)}
    it { should have_db_column(:end_date)}
  end
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-24T00:32:25+00:00Added an answer on May 24, 2026 at 12:32 am

    I would rather have this tested using a query and checking the results, but if you really must do it, one possible solution would be something like this for Rails 3:

    CatMembership.scoped.to_sql.should == CatMembership.order(:created_at).to_sql
    

    And on Rails 2:

    CatMembership.default_scoping.should == [{:create=>{}, :find=>{:order=>"created_at"}}]
    

    But I would not say these solutions are ideal since they show a lot of knowledge of the implementation (and you can see the implementation varies with different Rails versions).

    Creating sample data, running an usual all query and checking the result is correctly ordered might have been simpler, would be closer to real unit testing and would work even as you upgrade your rails version.

    In this case it would possibly be:

    before do
      @memberships = []
    
      @memberships << CatMembership.create!
      @memberships << CatMembership.create!
      @memberships << CatMembership.create!
    
      [ 1.hour.ago, 5.minutes.ago, 1.minute.ago ].each_with_index do |time, index|
        membership = @memberships[index]
        membership.created_at = time
        membership.save
      end
    
    end
    
    it 'should be correctly ordered' do
      @sorted_memberships = CatMembership.all
      @memberships.first.should == @sorted_memberships.last
      @memberships.second.should == @sorted_memberships.second
      @memberships.third.should == @sorted_memberships.first
    end
    

    It’s much more verbose, but it’s going to work even as you move forward on rails.

    And now I have just noticed who asked the question 😀

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

Sidebar

Related Questions

My Invoice model has an address_id attribute, and I don't want this address_id to
The GoDiagram object model has a GoDocument. GoViews have a reference to a GoDocument.
The standard model has been that OpenGL is for professional apps (CAD) and Direct3D
I have a Person model that has a foreign key relationship to Book ,
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
Java has generics and C++ provides a very strong programming model with template s.
My application has a plug-in model that allows third-party developers to write assemblies that
I have a class that has a Generic type G In my class model
My problem right now is that a Model has a set of Validation rules

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.