【问题标题】:Taking cart id and putting into order table获取购物车 ID 并放入订单表
【发布时间】:2014-04-14 11:15:27
【问题描述】:

我有模型 order.rb

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 并将其放入订单表的列中?

谢谢

迈克尔

【问题讨论】:

  • 根据给定的关联,您的order 表中不会有cart_id。您的cart 表中将有order_id。您只是感到困惑。您必须阅读这些指导guides.rubyonrails.org/…

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord model-associations


【解决方案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

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 2017-10-11
    • 1970-01-01
    • 2011-11-27
    • 2013-12-13
    • 2017-04-27
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多