2014-09-03 7 views
0

몇 가지 모델을 생성하고 다음과 같은 마이그레이션 파일을 생성했습니다. 2 차 마이그레이션에서 2 가지 참조 유형이 표시됩니다. sub_configuration_id는 item_configurations_model에 대한 참조입니다. 선택적 참조 (NULL 일 수 있음)입니다.레일스 모델 참조 형식을 생성

ItemConfigurationOption 모델을 살펴보면 다음과 같이 나타났습니다. belongs_to :sub_configuration_id. 이것은 belongs_to : sub_configuration_id가 모델이 아니기 때문에 유효하지 않습니다. sub_configuration_id에 대한 가능한 관계를 어떻게 참조해야합니까? 당신이 ItemConfiguration에 관계를 선언 할 수 있습니다 위의 구문을

belongs_to :sub_configuration, :class_name => :ItemConfiguration, :foreign_key => :sub_configuration_id 

:에

class ItemConfigurationOption < ActiveRecord::Base 
    belongs_to :item_configuration 
    belongs_to :sub_configuration_id 
end 

class CreateItemConfigurations < ActiveRecord::Migration 
    def change 
    create_table :item_configurations do |t| 
     t.references :item, index: true 
     t.string :name 
     t.string :description 
     t.integer :type 

     t.timestamps 
    end 
    end 
end 


class CreateItemConfigurationOptions < ActiveRecord::Migration 
    def change 
    create_table :item_configuration_options do |t| 
     t.references :item_configuration, index: true 
     t.references :sub_configuration_id, index: true 
     t.string :name 
     t.string :value 
     t.decimal :price 

     t.timestamps 
    end 
    end 
end 

답변

1

변경 번째 줄. 그런 다음 sub_configuration 메소드를 사용하여 하위 구성 객체를 생성 할 수 있습니다.

+0

알았습니다! 참고 자료 ... 참조 데이터 유형은 실제로 의미/할 무엇입니까? 이 경우에 적절합니까? –