2010-07-08 3 views
0

어떤 이유에서 아래의 seed.rb 파일을 실행할 때 '등급'의 food_id 필드가 채워지지 않습니다. 왜 누군가가 나를 알아낼 수 있습니까? 음식과 평가에레일 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

+0

은 마이그레이션 코드가 정확하게 강조 표시되지 않는 것처럼 보이며, 들여 쓰기를 빠르게 수정하고 수정하려고 할 수 있습니다. – mportiz08

답변

0

어떻게 seeds.rb 파일을 호출합니까? rake db:seed

+0

rake db : 마이그레이션 : 재설정 rake db : seed 결과는 같습니다. – Tian

0

정말 실제 코드입니까? 등급/OverallRating에 attr_accessible 또는 attr_protected 선언이있어 인스턴스화 된 객체에서 옵션이 설정되지 않도록합니다.

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

난 그냥하지만 ​​정확한 방법을 시도하고 문제없이 나를 위해 일한 :

+0

예 실제 코드입니다. 모든 attr_accessible 및 attr_protected 선언을 제거했지만 여전히 업데이트되지 않습니다 ... – Tian

+1

여기에 모델이 평가라고 할 때이 코드가 OverallRating을 참조하는 이유는 무엇입니까? 이 두 테이블에 대한 라이브 DB 스키마를 게시 할 수 있습니까? – Winfield

0

대신 id을 하드 코딩, 이런 식으로 뭔가를하려고합니다. 그래서 아마도 당신은 뭔가 다른 것이 있습니다.

관련 문제