覆盖Rails Uploader以播种数据库 [英] Override Rails Uploader to seed database

查看:55
本文介绍了覆盖Rails Uploader以播种数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CarrierWave导轨上传器。我想用假用户为数据库添加种子,所以我试图使用相同的种子文件添加图像。图片存储在公共存储区中,因此,只要我能在数据库中获取头像字符串,它们就可以使用。

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 => "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> 


推荐答案

您将必须以以下方式插入数据

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 => "test1@test.com", :password => "testing", :name => "Bob Dylan", :avatar => open("v1357014344/bdylan.jpg")

相关问题

这篇关于覆盖Rails Uploader以播种数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆