2010-07-08 50 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) 

我只是想你確切的方法,雖然,它的工作對我來說沒有問題。所以也許你有其他的錯誤。