0
我生成了幾個模型,並創建了以下遷移文件。在第二次遷移中,您將看到2個引用類型。 sub_configuration_id是對item_configurations_model的引用。這是一個可選的參考(它可以是NULL)。rails生成模型引用類型
當我看着ItemConfigurationOption模型時,我注意到以下belongs_to :sub_configuration_id
。這是無效的,因爲belongs_to:sub_configuration_id不是模型。我應該如何引用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
Got it!在附註中......引用數據類型實際上意味着什麼?這在這種情況下適合嗎? – 2014-09-03 18:30:51