Tuesday, July 23, 2024
HomeRuby On RailsWhat Does Load_defaults Do? - FastRuby.io

What Does Load_defaults Do? – FastRuby.io

Let’s start with a easy query:

Have you ever seen config.load_defaults 6 in any utility?

6 is an argument and will fluctuate, I’m simply giving an instance.

Whereas upgrading a Rails utility from 5.1 to six.0 we had an “simple” query.

Ought to We Run the rails app:replace Command?

The fast reply is: sure, we should always, why not?!

After working the rails app:replace command,
there are a lot of file modifications that the command did for us.

It was time to run the take a look at suite, and guess what? Every part failed.
The following step ahead was to undo all of the modifications and begin with a single configuration change we thought could be simple and secure.
Then we added the config.load_defaults 6.0 line and ran the take a look at suite once more.
It was nonetheless failing.

Simply to double-check, we commented that line again and ran the take a look at suite.
Every part was passing, in order that little line was ruining every thing.

We determined to deal with config.load_defaults 6.0.

We looked for def load_defaults in rails core
to see what load_defaults does.
Take your time to know it.

Tip: You possibly can simply navigate the rails repository utilizing https://github.dev/rails/rails
Extra about GitHub editor

If you’re not in a position to navigate to the rails core code, I added the load_defaults methodology beneath.

  # rails/railties/lib/rails/utility/configuration.rb

  def load_defaults(target_version)
    case target_version.to_s
    when "5.0"
      # rails 5.0 default configuration
    when "5.1"
      load_defaults "5.0"
      # rails 5.1 default configuration
    when "5.2"
      load_defaults "5.1"
      # rails 5.2 default configuration
    when "6.0"
      load_defaults "5.2"
      # rails 6 default configuration
    when "6.1"
      load_defaults "6.0"
      # rails 6.1 default configuration
    when "7.0"
      load_defaults "6.1"
      # rails 7 default configuration
    when "7.1"
      load_defaults "7.0"
      # rails 7.1 default configuration
      increase "Unknown model #{target_version.to_s.examine}"

    @loaded_config_version = target_version

The very first thing that caught my consideration was the truth that it calls the identical
load_defaults methodology recursively with the earlier rails model as an argument.

def load_defaults(target_version)
  case target_version.to_s
  when "5.1"
    load_defaults "5.0" # right here
  when "5.2"
    load_defaults "5.1" # right here
  when "6.0"
    load_defaults "5.2" # right here

Which means it should load the default configuration for the model you indicated and likewise all of the earlier variations as a fallback.

Should you use config.load_defaults("6.1") in your Rails utility, it should implement load_defaults for model 6.0, 5.2, 5.1 and 5.0 as a fallback.

Which is sweet, since you received’t want so as to add all these configuration traces in your personal undertaking.

What Does load_defaults Do?

In easy phrases, the load_defaults methodology is loading default rails configurations
ranging from Rails 5 as much as Rails 7.1 (because it’s presently the newest model).
You possibly can examine every configuration and what they
do over right here.

So, Why Does My Software Fail?

After reviewing the appliance,
we observed config.load_defaults had been eliminated in a earlier replace,
once we added it again, it threw an error as a result of the load_defaults methodology for
the present model of Rails enabled config.belongs_to_required_by_default = true

Altering config.belongs_to_required_by_default to false did the trick to resolve the problem.

Ought to We Use It?

That is such a terrific query, the short reply is possibly! LOL.

The longer reply is:
How have you ever used the rails configurations earlier than?
If in case you have at all times been utilizing the load_defaults config,
it appears to be secure to make use of in your subsequent replace.

Should you haven’t used it, first ensure to learn each single rule within the
default values part
and double-check your utility is suitable with all of them.

I hope this may also help you sooner or later, till subsequent time.

You solely fail whenever you cease attempting – Unknown



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments