nachime
nachime

Reputation: 1856

How to add data to rails app in production?

I'm about to add in a few users manually (because I want to make them admins eg user.admin = true. Previously, in development I've done this using seeds but I believe (correct me if I'm wrong) that you aren't supposed to use seeds in this way in production. What else should I do?

Upvotes: 0

Views: 78

Answers (2)

Okomikeruko
Okomikeruko

Reputation: 1173

Do you have access to the production command line? If you're using Heroku you can do this:

$ heroku run rails console
> user = User.new
> user.name = "The name"
// repeat for all other fields
> user.admin = true
> user.save

Or if you already have them in the database as non-admins

$ heroku run rails console
> user = User.find_by(name: "Their Name")
> user.update_attribute(:admin, true)

Upvotes: 0

Matt
Matt

Reputation: 646

There are a number of options, each with their own pros and cons.

  • Set up the users in the Rails console
    • Advantage: simple, can customize as much as needed
    • Disadvantage: not reproducible
  • Add users in a migration
    • Advantage: if this is a new table or relevant data exists in other tables, you can set up the new data right away as part of a migration
    • Disadvantage: not a standard use of migration scripts
  • Set up users with a rake task
    • Advantage: code to reproduce your steps will be checked into your repo
    • Disadvantage: if you only want to do this once, a rake task is overkill

The best solution might be to add a script in the scripts/ directory of your app detailing the steps, have this code reviewed by your team, and then run it with rails runner.

Upvotes: 2

Related Questions