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?

Reply

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: "test@example.com", 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!

Reply

Awesome, thanks so much Chris!!

Reply
Join the discussion
Create an account Log in

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

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

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