2010-07-08 49 views
0

由于某些原因,当我运行下面的seed.rb文件时,'ratings'中的food_id字段未填充。有人可以帮我弄清楚为什么?Rails DB播种

种子文件包含以下行:

Food.create(:id => 1, :description => 'Stonyfield Farm Yomommy 4oz. Strawberry') 
OverallRating.create(:score => 0, :count => 1, :food_id => 1) 

规范食品和评级如下: 类OverallRating <评级 belongs_to的:食品 结束

class Food < ActiveRecord::Base 
    has_one :overall_rating 
end 

class Rating < ActiveRecord::Base 
    belongs_to :food 
end 

该评级迁移文件如下:

class CreateRatings < ActiveRecord::Migration 
    def self.up 
    create_table :ratings do |t| 
     t.integer :food_id 
     t.integer :count 
     t.decimal :score 
     t.string :type 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :ratings 
    end 
end 
+0

我有一种感觉,这与我在评分和总体评分中使用单表继承有关。但不能肯定... – Tian 2010-07-08 01:48:11

+0

貌似迁移代码没有被正确突出,可能需要编辑和修正压痕真正的快速 – mportiz08 2010-07-08 01:49:23

回答

0

你是如何调用seeds.rb文件的?你可能需要做一个rake db:seed

+0

我调用: 耙分贝:迁移:重置 耙分贝:种子 结果是一样的 – Tian 2010-07-08 01:47:26

0

这真的是真正的代码?我猜你已经在Rating/OverallRating中获得了attr_accessible或attr_protected声明,这会阻止在实例化对象中设置该选项。

+0

是的,这是实际的代码。我删除了所有attr_accessible和attr_protected声明,但它仍然不更新... – Tian 2010-07-08 01:46:40

+1

为什么此代码引用OverallRating时,此处的模型称为Rating? 你可以发布这两个表的实时数据库模式吗? – Winfield 2010-07-08 02:10:47

0

而是硬编码id的,尝试这样的事情:

food = Food.create(:description => 'blah') 
food.create_overall_rating(:score => 0, :count => 1) 

我只是想你确切的方法,虽然,它的工作对我来说没有问题。所以也许你有其他的错误。