我正在尝试为产品创建种子数据,但是产品的价格区域由价格模型中的一系列价格填写。
当我按如下方式填写种子文件时,我收到一条错误消息,说“预期价格,得到一个字符串”。我理解为什么会这样,但我不知道如何填写种子数据中的价格字段。
我已经查看了与此类似的其他问题但仍然遇到错误
以下是我的产品种子在'got a String'错误
时的样子if !Product.exists?(:product_title => 'test7')
Product.create(
product_title: 'test7',
product_desc: 'Available in a range of colours and designs.',
price: '9.99',
department: 'Accessories',
display_on_home_page: true,
is_highlight_product: false,
start_date: '13/07/2013')
end
price有一个'attached_file'和一个作为字符串输入的'value'。
我知道我已经接近但无法正确使用语法,非常感谢任何帮助。
编辑1
价格模型
class Price < ActiveRecord::Base
has_many :products
attr_accessible :value, :image_attachment, :price_id
attr_accessor :image_file_name
attr_accessor :image_content_type
attr_accessor :image_file_size
attr_accessor :image_updated_at
has_attached_file :image_attachment,
:styles => {
:normal_page_size => "81x85>",
:large_page_size => "140x140#"
},:default_url => "/assets/missing_images/:style/missing.png"
def image_url
if self.image.nil?
"/assets/thumb_sq/missing.png"
else
self.image_attachment.url(:normal_page_size)
end
end
end
答案 0 :(得分:1)
而不是price: '9.99'
做price: Price.create(value: '9.99')