Rails for a single row table

Are there any rail conventions or the right way to create / manipulate a table containing only one row? If not, what is the best way to do this? I need a way to store system configurations.

Thanks.

+5
source share
1 answer

Edited by:

The rake db:seed command basically executes any code that you write in your application's db/seeds.rb file. Although you can write any code in this file, by convention you should write code that populates the database with basic data,

for example: when you ever deploy your application and create a new database for it, you want this user with administrator credentials to be present there. Thus, you will write the code that will create this user in this file. The following is sample code that will create the user and assign him the administrator role.

 puts "********Seeding Data Start************" admin = User.create(:first_name => 'System', :last_name => 'Admin', :email => ' systemadmin@sunpower.com ', :password => 'sunpoweradmin', :password_confirmation => 'sunpoweradmin', :source_system_id => 'systemadmin', :source_system => 'LP',:entity_type => "Customer", :target_system => "OPENAM") if admin.errors.blank? puts "***User #{admin.first_name} #{admin.last_name} created ***" admin.add_role :admin # add_role is method defined by rolify gem puts "***admin role assigned to #{admin.first_name} #{admin.last_name}***" else puts "admin user failed to create due to below reasons:" admin.errors.each do |x, y| puts"#{x} #{y}" # x will be the field name and y will be the error on it end end puts "********Seeding Data End************" 

Now, when you recreate your database, you just need to run the command below to populate the database, with basic data

$ rake db:seed RAILS_ENV=production

The correct database installation procedure during production, with the entire rake task available in the db namespace below

 $rake db:create RAILS_ENV=production $rake db:migrate RAILS_ENV=production $ rake db:seed RAILS_ENV=production 

NOTE. . You can replace the first two commands with $rake db:setup RAILS_ENV=production , it will be run both for creation and for migration inside


OR

You can use rails-settings-cached gem , which is the fork of the rails settings graph

After setting up, you can do things like:

 Setting.foo = 123 Setting.foo # returns 123 

Hope this helps you or what you are looking for.

+1
source

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


All Articles