4

我有一个 CarrierWave rails 上传器。我想用假用户为数据库播种,所以我试图用相同的种子文件添加图像。这些图像在一个公共存储中,所以如果我可以在数据库中获取头像字符串,它们就可以工作。当它保存用户时,虽然图像没有粘住。

# 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

3 回答 3

4

您必须按以下方式插入数据。

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

所以整个用户创建看起来像

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

相关问题

于 2013-01-01T05:58:46.417 回答
3

除了open()用作Nishant建议外,您还可以指定remote_avatar_url手动设置远程 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"

感谢JosephJaber使用 CarrierWave、Rails 3 播种文件上传时提出的建议

我使用这种方法在我的seeds.rb 中为 CarrierWave Uploader 填充了一些视频 URL

于 2013-10-14T20:56:43.427 回答
1

To update the database directly and skip out on CarrierWave:

model[:attribute] = 'url.jpg'
model.save
于 2015-03-16T21:18:45.023 回答