Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Rails Uploader to seed database

I have a CarrierWave rails uploader. I want to seed the database with fake users so I'm trying to add the images in with the same seed file. The images are in a common storage, so if I can just get the avatar strings in the database they'll work. When it saves the users though the image's aren't sticking.

# db/seeds.rb
user1 = User.create :email => "[email protected]", :password => "testing", :name => "Bob Dylan", :avatar => "v1357014344/bdylan.jpg"
user1.save

# IRB
User.first
=> #<User id: 1, email: "[email protected]", name: "Bob Dylan", avatar: nil> 

> a = User.first
> a.avatar = "v1357014344/bdylan.jpg"
> a.save
 (10.7ms)  commit transaction
=> true 
> a
=> #<User id: 1, email: "[email protected]", name: "Bob Dylan", avatar: nil> 
like image 367
Robert Avatar asked Feb 18 '23 00:02

Robert


1 Answers

You will have to insert the data in the following way.

File.open(File.join(Rails.root, 'test.jpg'))

So the entire user create would look like

User.create :email => "[email protected]", :password => "testing", :name => "Bob Dylan", :avatar => open("v1357014344/bdylan.jpg")

Related question

like image 79
Nishant Avatar answered Mar 03 '23 18:03

Nishant