Finish loading Rails into the seed database

I have a CarrierWave rail loader. I want a seed database with fake users, so I am trying to add images to the same seed file. Images are in shared storage, so if I can just get the avatar strings in the database, they will work. When it saves users, although the image does not stick.

# db/seeds.rb user1 = User.create :email => " test1@test.com ", :password => "testing", :name => "Bob Dylan", :avatar => "v1357014344/bdylan.jpg" user1.save # IRB User.first => #<User id: 1, email: " test1@test.com ", 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: " test1@test.com ", name: "Bob Dylan", avatar: nil> 
+4
source share
3 answers

You will need to insert the data as follows.

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

Thus, everything created by the user will look like

 User.create :email => " test1@test.com ", :password => "testing", :name => "Bob Dylan", :avatar => open("v1357014344/bdylan.jpg") 

Question related to us

+4
source

Besides using open() as Nishant , you can also specify remote_avatar_url to manually set the remote URL.

 User.create :email => " test1@test.com ", :password => "testing", :name => "Bob Dylan", :remote_avatar_url => "http://upload.wikimedia.org/wikipedia/commons/2/28/Joan_Baez_Bob_Dylan_crop.jpg" 

Thanks to JosephJaber for suggesting this in Downloading Downloads with CarrierWave, Rails 3

I used this approach to populate some video URLs for the CarrierWave downloader in my seeds.rb

+3
source

To update the database directly and upgrade to CarrierWave:

 model[:attribute] = 'url.jpg' model.save 
+1
source

All Articles