Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to seed a database in Rails?
    primarykey
    data
    text
    <p>I have a rake task that populates some initial data in my rails app. For example, countries, states, mobile carriers, etc.</p> <p>The way I have it set up now, is I have a bunch of create statements in files in /db/fixtures and a rake task that processes them. For example, one model I have is themes. I have a theme.rb file in /db/fixtures that looks like this:</p> <pre><code>Theme.delete_all Theme.create(:id =&gt; 1, :name=&gt;'Lite', :background_color=&gt;'0xC7FFD5', :title_text_color=&gt;'0x222222', :component_theme_color=&gt;'0x001277', :carrier_select_color=&gt;'0x7683FF', :label_text_color=&gt;'0x000000', :join_upper_gradient=&gt;'0x6FAEFF', :join_lower_gradient=&gt;'0x000000', :join_text_color=&gt;'0xFFFFFF', :cancel_link_color=&gt;'0x001277', :border_color=&gt;'0x888888', :carrier_text_color=&gt;'0x000000', :public =&gt; true) Theme.create(:id =&gt; 2, :name=&gt;'Metallic', :background_color=&gt;'0x000000', :title_text_color=&gt;'0x7299FF', :component_theme_color=&gt;'0xDBF2FF', :carrier_select_color=&gt;'0x000000', :label_text_color=&gt;'0xDBF2FF', :join_upper_gradient=&gt;'0x2B25FF', :join_lower_gradient=&gt;'0xBEFFAC', :join_text_color=&gt;'0x000000', :cancel_link_color=&gt;'0xFF7C12', :border_color=&gt;'0x000000', :carrier_text_color=&gt;'0x000000', :public =&gt; true) Theme.create(:id =&gt; 3, :name=&gt;'Blues', :background_color=&gt;'0x0060EC', :title_text_color=&gt;'0x000374', :component_theme_color=&gt;'0x000374', :carrier_select_color=&gt;'0x4357FF', :label_text_color=&gt;'0x000000', :join_upper_gradient=&gt;'0x4357FF', :join_lower_gradient=&gt;'0xffffff', :join_text_color=&gt;'0x000000', :cancel_link_color=&gt;'0xffffff', :border_color=&gt;'0x666666', :carrier_text_color=&gt;'0x000000', :public =&gt; true) puts "Success: Theme data loaded" </code></pre> <p>The idea here is that I want to install some stock themes for users to start with. I have a problem with this method.</p> <p>Setting the ID does not work. This means that if I decide to add a theme, let's call it 'Red', then I would simply like to add the theme statement to this fixture file and call the rake task to reseed the database. If I do that, because themes belong to other objects and their id's change upon this re-initialization, all links are broken. </p> <p>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. </p> <p>If so, how can I hard code the IDs, and are there any downsides to that?</p> <p>If not, what is the best way to seed the database?</p> <p>I will truly appreciate long and thought out answers that incorporate best practices.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload