2011-10-19 110 views
0

我是一個完整的rails新手,所以如果這是微不足道的,請原諒我。相關模型沒有被創建

我有,要麼belongs_to的商店或參加旅遊的庫存模型:

class Inventory < ActiveRecord::Base 
    belongs_to :trader, :polymorphic => true 
end 

class Store < ActiveRecord::Base 
    has_one :inventory, :as => :trader, :dependent => :destroy 
end 

class TravelingParty < ActiveRecord::Base 

    has_many :travelers, :dependent => :destroy 
    has_one :inventory, :as => :trader, :dependent => :destroy 
    validates_presence_of :speed, :ration, :position 
    accepts_nested_attributes_for :travelers, :reject_if => :reject_traveler, :allow_destroy => true 
    accepts_nested_attributes_for :inventory, :allow_destroy => true 

    def reject_traveler(attributes) 
     attributes['profession'].blank? and attributes['name'].blank? 
    end 

end 

我創建了一個表單,提交時,創建了一個旅行黨和許多遊客。現在我希望表單也可以創建一個Inventory並將所有變量初始化爲0.我知道以下內容不涉及變量初始化,但它甚至似乎沒有將一行空值放入庫存數據庫表中。

class TravelingPartiesController < ApplicationController 

    def new 
     @traveling_party = TravelingParty.new 
     5.times do 
      traveler = @traveling_party.travelers.build 
     end 
     @inventory = @traveling_party.inventory.create 

    end 

    def create 
     @traveling_party = TravelingParty.new(params[:traveling_party]) 
     if @traveling_party.save 
      flash[:notice] = "Successfully created traveling party and travelers." 
      redirect_to '/store/' 
     else 
      flash[:error] = "Please specify a leader." 
      redirect_to '/new/' 
     end 
    end 

    def index 
    end 

end 

良好的措施,這裏是數據庫架構的樣子:

ActiveRecord::Schema.define(:version => 20111018224808) do 

    create_table "inventories", :force => true do |t| 
    t.integer "ox" 
    t.integer "food" 
    t.integer "clothing" 
    t.integer "ammunition" 
    t.integer "money" 
    t.integer "axle" 
    t.integer "wheel" 
    t.integer "tongue" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "trader_id" 
    end 

    create_table "stores", :force => true do |t| 
    t.string "name" 
    t.integer "location" 
    t.integer "priceScale" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

# Could not dump table "travelers" because of following StandardError 
# Unknown type 'relations' for column 'traveling_party_id' 

    create_table "traveling_parties", :force => true do |t| 
    t.integer "speed" 
    t.integer "ration" 
    t.integer "position" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

是否有庫存數據庫表不被在所有受影響的理由嗎?一旦有效,初始化travelling_party.inventory以獲得全0的最佳方式是什麼? (即,牛,食物,衣服等的值)。

+0

遷移,這看起來可疑:'#莫非不轉儲表「旅客」,因爲以下StandardError #未知類型'關係'列'traveling_party_id'' – Matthew

回答

0

這可能是因爲您的庫存表不包含「trader_type」。這是多態關聯所必需的。

create_table "inventories", :force => true do |t| 
    t.integer "trader_id" 
    t.string "trader_type" 
    end 

編輯:

初始設置的所有值設爲0,最好的辦法是把默認值在上表中的字段。 (如果你想,如果沒有其他選擇,它總是被初始化爲0,否則會默認爲無)

我相信你可以創建

change_table(:inventories) do |t| 
    t.change :ox, :integer, :default => 0 
end 
+0

謝謝!我們最終實現了STI而不是多態 – jmaliakal