当我尝试使用db:seed时,我在rails控制台中得到了上面的错误:
=> #<ActiveRecord::Relation [#<Product id: 16, name: nil, price: nil, active: true, description: nil, image_url: nil, professor: nil, created_at: "2015-04-24 06:59:58", updated_at: "2015-04-24 06:59:58">, #<Product id: 17, name: nil, price: nil, active: true, description: nil, image_url: nil, professor: nil, created_at: "2015-04-24 06:59:58", updated_at: "2015-04-24 06:59:58">, #<Product id: 18, name: nil, price: nil, active: true, description: nil, image_url: nil, professor: nil, created_at: "2015-04-24 06:59:58", updated_at: "2015-04-24 06:59:58">]>
这是我的种子.rb:
Product.delete_all
Product.create( name: 'Montages simples ikebana en chocolat',
description:
%{
Créez des montages commerciaux simple et rapide pour mettre en valeur votre vitrine.
},
image_url: 'IMG_4543.JPG',
price: 9.00,
professor: 'avec Jérémy Fages', active: true)
Product.create( name: 'Example2',
description:
%{
Miam miam.
},
image_url: 'miam.jpg',
price: 'gratuit',
professor: 'avec Jérémy Fages', active: true)
这是我的*********** _ create_products.rb:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.decimal :price, precision: 12, scale: 3
t.boolean :active
t.text :description
t.string :image_url
t.text :professor
t.timestamps
end
end
end
和我的Model / products.rb:
class Product < ActiveRecord::Base
has_many :order_items
default_scope { where(active: true) }
end
我尝试了命令rake db:drop rake db:create rake db:migrate ...
这是我的Model / Order_item.rb:
class OrderItem < ActiveRecord::Base
belongs_to :product
belongs_to :order
validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }
validate :product_present
validate :order_present
before_save :finalize
def unit_price
if persisted?
self[:unit_price]
else
product.price
end
end
def total_price
unit_price * quantity
end
private
def product_present
if product.nil?
errors.add(:product, "is not valid or is not active.")
end
end
def order_present
if order.nil?
errors.add(:order, "is not a valid order.")
end
end
def finalize
self[:unit_price] = unit_price
self[:total_price] = quantity * self[:unit_price]
end
end
关于代码,它在使用sqlite3的应用程序上运行。问题出现在我使用mysql2的其他应用程序上。