Using config.load_defaults in bug reports

This is something that I just used, and found it surprisingly not straight-forward (or documented!) afaict.

So, I thought I’d share it here.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "sqlite3"
end

require 'rails'
require 'active_record/railtie'
require "minitest/autorun"
require "logger"

class TestApp < Rails::Application
  config.load_defaults "7.1"
  #config.active_record.run_after_transaction_callbacks_in_order_defined = true

  config.logger = Logger.new(STDOUT)
end

ENV["DATABASE_URL"] = "sqlite3::memory:"

Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table :books, force: true do |t|
    # add some fields to the `books` table
  end
end

class Book < ActiveRecord::Base
  # define some custom behavior to the `Book` model
end
  
class BugTest < Minitest::Test
  def test_it_does_a_thing
    b = Book.new

    assert_not book.persisted?
  end
end

Ignoring the test case, I just found the order in which these things are defined to be particularly sensitive, and I hadn’t seen an example (that I can remember) where defaults are being loaded. Usually I see test cases where the author manipulates that value directly in the test block.

Do you think this is useful? Or do you prefer to edit the flag inside the test?

I’m also wondering what config options are loaded prior to load_defaults :thinking:

1 Like

I’m using this kind of one file application for prototyping. Never occured to me that rails default would be different than the version I require in the gemfile part.

I would expect the defaults to match the rails version by default and match a specific version if explicitly set with config.load_defaults.

You can see here, for each setting, what’s the original/default and the new default for each Rails version if that’s what you are asking here.

You can find tables like this: Configuring Rails Applications — Ruby on Rails Guides