2015-11-04 112 views
0

我很努力,看看有什麼我做錯了這裏..態關聯未定義的方法`build_product」

我有需要能夠舉行一個產品的訂單模式,產品必須是多態的。

我有一個名爲orthosis_specification的產品/模型,出於某種原因,我在創建fields_for的過程中遇到此錯誤。

遷移 -

class CreateOrders < ActiveRecord::Migration 
    def change 
    create_table :orders do |t| 
     t.datetime :order_date 
     t.datetime :date_required 
     t.boolean :correct 
     t.references :user, index: true, foreign_key: true 
     t.references :practitioner, index: true, foreign_key: true 
     t.references :product, polymorphic: true 
     t.references :shipping_address, index: true, foreign_key: true 
     t.references :invoice_address, index: true, foreign_key: true 
     t.timestamps null: false 
    end 
    end 
end 

訂單控制器 -

def new 
    @order = Order.new 
    @order.build_patient 
    @order.build_product #Also tried: @order.build_orthosis_specification 
    end 

階模型 -

class Order < ActiveRecord::Base 
    has_one :patient 
    belongs_to :product, polymorphic: true 
    accepts_nested_attributes_for :patient, 
           reject_if: proc { |attributes| attributes['first_name'].blank? }, 
           allow_destroy: true 
    accepts_nested_attributes_for :product, 
           reject_if: proc { |attributes| attributes['transfer_name'].blank? }, 
           allow_destroy: true 

    def to_s 
    name 
    end 
end 

矯形器規格型號 -

class OrthosisSpecification < ActiveRecord::Base 
    has_one :order, :as => :product, class_name: 'Order' 
end 

順序查看 -

<%= form_for(@order) do |f| %> 
    <% if @order.errors.any? %> 
    <% end %> 

<%= f.fields_for :orthosis_specification do |fa| %> 

實際的錯誤信息 -

undefined method `build_orthosis_specification' for #<Order:0x007f8950e29970> 

矯形器規格遷移 -

class CreateOrthosisSpecifications < ActiveRecord::Migration 
    def change 
    create_table :orthosis_specifications do |t| 
     t.string :transfer_name 
     t.string :modifications 
     t.string :primary_mods 
     t.string :top_opening 
     t.string :side_opening 
     t.string :chape_position 
     t.string :scan_file 
    end 
    end 
end 

任何幫助,將大規模感激,謝謝!

+0

什麼是你的錯誤是什麼呢?你能否用錯誤信息更新你的問題? – Caillou

+0

當然,已經完成了。 – AndrewJL

+0

您能否爲矯形器規格表添加移植?我想我有你的問題,但我想確定。 – Caillou

回答

1

多態關聯不會生成build_xxx方法。您可以創建一個新的產品只有當你知道你想要創造什麼樣的產品:

#Creating a new OrthosisSpecification product associated with @order : 
@order.product = OrthosisSpecification.new 

文檔:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

+0

謝謝,但我已經在我的代碼,但:( – AndrewJL

+0

正確......我需要更換我的眼鏡x_x我沒有看到'belongs_to'在第一次 – Caillou

+0

你說你的錯誤是'未定義的方法'build_orthosis_specification' '......你用'build_product'得到了同樣的結果嗎? – Caillou

相關問題