I have a rake task that populates some source data in my rails application. For example, countries, states, mobile operators, etc.
As I configured it now, I have a bunch of creating statements in files in / db / fixtures and a rake task that handles them. For example, one model has themes. I have a theme.rb file in / db / fixtures that looks like this:
Theme.delete_all Theme.create(:id => 1, :name=>'Lite', :background_color=>'0xC7FFD5', :title_text_color=>'0x222222', :component_theme_color=>'0x001277', :carrier_select_color=>'0x7683FF', :label_text_color=>'0x000000', :join_upper_gradient=>'0x6FAEFF', :join_lower_gradient=>'0x000000', :join_text_color=>'0xFFFFFF', :cancel_link_color=>'0x001277', :border_color=>'0x888888', :carrier_text_color=>'0x000000', :public => true) Theme.create(:id => 2, :name=>'Metallic', :background_color=>'0x000000', :title_text_color=>'0x7299FF', :component_theme_color=>'0xDBF2FF', :carrier_select_color=>'0x000000', :label_text_color=>'0xDBF2FF', :join_upper_gradient=>'0x2B25FF', :join_lower_gradient=>'0xBEFFAC', :join_text_color=>'0x000000', :cancel_link_color=>'0xFF7C12', :border_color=>'0x000000', :carrier_text_color=>'0x000000', :public => true) Theme.create(:id => 3, :name=>'Blues', :background_color=>'0x0060EC', :title_text_color=>'0x000374', :component_theme_color=>'0x000374', :carrier_select_color=>'0x4357FF', :label_text_color=>'0x000000', :join_upper_gradient=>'0x4357FF', :join_lower_gradient=>'0xffffff', :join_text_color=>'0x000000', :cancel_link_color=>'0xffffff', :border_color=>'0x666666', :carrier_text_color=>'0x000000', :public => true) puts "Success: Theme data loaded"
The idea here is that I want to install some themes for users to get started. I have a problem with this method.
Setting the identifier does not work. This means that if I decide to add a theme, let her call it "Red", then I would just like to add a theme operator to this binding file and call the rake task to reload the database. If I do this because the topics are related to other objects and their identifier changes during this reinitialization, all links are broken.
My question is, first of all, is this a good way to handle seeding a database? In a previous post, this was recommended to me.
If so, how can I hardcode the identifiers and are there any flaws?
If not, what is the best way to plant a database?
I will truly appreciate the long and thoughtful answers that include best practices.