Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Structure your seed.rb file to allow ongoing creation and updating of data. You are not limited to running a seed file only once and if you think it's only used for initial deployment you will miss out on the flexibility it can offer in setting reference data.</p> <p>A seed file is just ruby so you can do things like:</p> <pre><code>user = User.find_or_initialize_by(email: 'bob@example.com') user.name = 'Bob' user.password = 'secret' user.role = 'manager' user.save! </code></pre> <p>This will create new data if it doesn't exist or update the data if it finds some.</p> <p>If you structure your seed file correctly you can also create and update dependent objects.</p> <p>I recommend using the bang save to ensure that exceptions are raised in the event that an object cannot be saved. This is the easiest method of debugging the seed.</p> <p>I use the <a href="https://github.com/james2m/seedbank" rel="noreferrer">seedbank gem</a> to provide more structure to my seed data, including setting data per environment, dependent seeds and more.</p> <p>I don't recommend using migrations for seed data. There is a lack of flexibility (how do you target seed data to just one environment for instance) and no real way to build up a reusable set of data that can be run at any time to refresh a particular environment. You would also have a set of migrations which have no reference to your schema and you would have to create new migrations every time you wanted to generate new or vary current data.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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