All threads / Some good ways of seeding data in my app?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Some good ways of seeding data in my app?

karl asked in Databases

I need some seed data for my app. How do I seed Devise users, and always as images for Paperclip?

I'd probably toss everything in db/seeds.rb. Check out the ffaker gem for creating fake names and things.

If you need to create Devise users, you can just do this (but you'll have to give them passwords)

# You can optionally generate a password if you don't want to hardcode the password
# generated_password = Devise.friendly_token.first(8)

User.create(email: "[email protected]", password: "password", password_confirmation: "password")

And for paperclip images, you can use the ruby File library to open the file and assign it.

img = File.open(File.join(Rails.root, 'image.png'))
User.first.update(avatar: img)

You can use Rails.root there as a helper to get the Rails directory, and then you can join in any folders and filenames you need to get the full path in Ruby. Then you open it as a File object and assign that to your paperclip attribute. That should do the trick!

Awesome, thanks so much Chris!!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,885+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.