0

我正在尝试为产品创建种子数据,但产品的价格字段由价格模型从一系列价格中填充。

当我按如下方式填写种子文件时,我收到一条错误消息,提示“预期价格,得到一个字符串”。我理解为什么会发生这种情况,但我不知道如何在我的种子数据中填写价格字段。

我已经查看了与此类似的其他问题,但不断收到错误

这是我的产品种子目前的样子,出现“得到一个字符串”错误

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
4

1 回答 1

1

而不是price: '9.99'price: Price.create(value: '9.99')

于 2013-11-26T13:07:38.393 回答