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

  • Home
  • SEARCH
  • 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 7674379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:43:29+00:00 2026-05-31T16:43:29+00:00

ActiveRecord::Base has a big ol’ API with multiple methods for both finding and saving

  • 0

ActiveRecord::Base has a big ol’ API with multiple methods for both finding and saving objects. For example, your AR::B objects might have been instantiated from a number of methods:

  • Foo.new(…)
  • Foo.create(…)
  • Foo.find(…)
  • Foo.find_by_sql(…)
  • Foo.find_[all_]by_*(…)
  • bar.foos (associations)
    • …and finder methods on associations, of course

Similarly, the object in question might get persisted by a few different methods:

  • foo.create or foo.create!
  • foo.save or foo.save!
  • foo.update_attributes or foo.update_attributes!

Now, when writing unit tests, it’s good practice to stub external method calls so that your test can focus on the business logic of the method in question. However, when it comes to working with AR::B objects – for example in controller unit tests – it seems like you have to commit to one of the above methods, when actually as far as the business logic of the method is concerned it shouldn’t be important which you choose.

Do you have to couple the behaviour of your method this tightly with its implementation or am I missing something simple?

  • 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-31T16:43:30+00:00Added an answer on May 31, 2026 at 4:43 pm

    One approach is to build your classes in such a way that you wrap up any ActiveRecord::Base method calls in your own methods.

    So instead of calling Foo.new(…) directly…

    class Foo < ActiveRecord::Base
      def self.create_object(…)
        new(…)
      end
    end
    

    That way in your tests you can stub out your own methods instead of ActiveRecord’s.

    This approach (including its’ benefits) is outlined in detail by Avdi Grimm in the book ‘Objects On Rails’… http://objectsonrails.com

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

Sidebar

Related Questions

My system has the following associations set up: class Book < ActiveRecord::Base has_many :
I'm inheriting code which has: class Graphic < ActiveRecord::Base has_many :comments, :foreign_key => 'asset_id',
My model looks like this (example): class Bank < ActiveRecord::Base has_and_belongs_to_many :currencies # other
I have a model: class Item < ActiveRecord::Base Item has a property 'store' Based
Given this example: class Server < ActiveRecord::Base has_many :clients,:dependent => :destroy after_destroy: delete_server_directory end
class User < ActiveRecord::Base has_attached_file :photo, :styles => { :square => 100%, :large =>
class Photo < ActiveRecord::Base has_many :boosts, class_name: BoostedPhoto ... end class BoostedPhoto < ActiveRecord::Base
class Assembly < ActiveRecord::Base has_and_belongs_to_many :parts end class Part < ActiveRecord::Base has_and_belongs_to_many :assemblies end
Two models: class Task < ActiveRecord::Base has_many :subtasks end class Subtask < ActiveRecord::Base belongs_to
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles

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.