0

我有模型order.rb以車ID和投入順序表

class Order < ActiveRecord::Base has_one :cart end

和模型cart.rb

class Cart < ActiveRecord::Base 
    include SecurelyPermalinkable 
    belongs_to :user 
    belongs_to :order 

end 

我如何可以採取cart_id和把它放在訂單表的列中?

感謝

邁克爾

+5

對於每個給出的關聯,你不會在你的'order' table.You將有'order_id'有'cart_id'在你的「購物車」表格中。你只是感到困惑。你必須閱讀這些指南http://guides.rubyonrails.org/association_basics.html#the-belongs-to-association – Pavan

回答

1

就像我說的,你應該做這樣的方式,你會在你的cart爲表order_id因爲你的協會都設置這樣的

如果你真的想cart_id在你的order表中,那麼你必須改變你的關聯。

訂貨型號

class Order < ActiveRecord::Base 
    belongs_to :cart 
end 

車模型

class Cart < ActiveRecord::Base 
     include SecurelyPermalinkable 
     belongs_to :user 
     has_one :order 

    end