Filling a table with constant values

In a Rails application, I need a table in my database to contain persistent data.

The contents of this table are not intended to be changed at this time, but I do not want to put the contents in code to be able to modify it if necessary.

I tried to populate this table in the migration that created it, but it does not seem to work with the test environment and does not break my unit tests. In a test environment, my model can never return any value, while this is normal in my development environment.

Is there a way to populate this database correctly even in a test environment? Is there any other way to process data of this type that should not be in the code?

change

Thanks for your answers, and especially Vlad R for explaining the problem.

Now I understand why my data does not load into the test. This is because the test environment uses the db:loadrake command , which directly loads the circuit instead of starting the migration. By putting my values ​​only in the migration, and not in the schema, these values ​​are not loaded for testing.

+3
source share
2 answers

What you are probably observing is that no migrations are performed in the test environment (db: migrate), but db / schema.rb (db: load) is loaded instead.

You have two options:

  • continue to use migration for production and development; for a test environment, add your persistent data to the corresponding yml files in db / fixtures.
  • db/fixtures yml ( ) , db/fixtures, , rake db:

, db: load ( db: migrate - , test, db: load db: migrate ..) - drop-in rakefile RAILS_APP/lib/tasks, db: load, "" yml ( ) .

db: seed rake . db/seed/.yml

#the command is: rake:db:load
namespace :db do
  desc 'Initialize data from YAML.'
  task :load => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/seeds/*.yml').each do |file|
      Fixtures.create_fixtures('db/seeds', File.basename(file, '.*'))
    end
  end
end

(db: migrate), one, , , .

- , , , , ..

+3

.

. rake, db: populate, ActiveRecord. test_helper. , , , .

SeedFu, .

, , , , .

+2

Source: https://habr.com/ru/post/1703592/


All Articles