Ruby on rails 无法显示来自用户的已订购项目
我有以下型号: product.rbRuby on rails 无法显示来自用户的已订购项目,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有以下型号: product.rb class Product has_many :purchases has_many :line_items has_many :orders, :through => :order_products class OrderProduct < ActiveRecord::Base belongs_to :order belongs_to :product end lineitem.rb class LineItem bel
class Product
has_many :purchases
has_many :line_items
has_many :orders, :through => :order_products
class OrderProduct < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
lineitem.rb
class LineItem
belongs_to :product
belongs_to :cart
belongs_to: order
order.rb
class Order
belongs_to :user
belongs_to :product
has_many :purchases
has_many :line_items, :dependent => :destroy
has_many :orders, :through => :order_products
purchase.rb
class Purchase
belongs_to :order
belongs_to :product
更新:
order_product.rb
class Product
has_many :purchases
has_many :line_items
has_many :orders, :through => :order_products
class OrderProduct < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
以上是我对模型的联想。但是,我在显示用户提供的产品时遇到了问题。成功购买物品后,将销毁第_行物品
有人知道我如何将所有购买的物品存储到purchase或其他更好的方法中,以显示用户购买的产品吗
我最初尝试检索行_项,结果成功了。但是,在销毁行_项目后,我无法检索相关产品
感谢您的帮助。您需要查看关联
User
has_many :orders
has_many :products, through: :orders
Order
has_many :line_items
has_many :products, through: :line_items
您可以尝试为每个用户创建
订单\u历史记录
:
user.rb
def order_history
#set this up according to how your models are related, etc
#with the idea being to call @user.order_history and getting an array of previous orders
end
if @order.save
if @order.purchase
#adapt this pseudocode to suit your needs and
#according to how you've defined `@user.order_history
current_user.order_history = current_user.order_history + @order.line_items
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
然后在order\u controller.rb中
def order_history
#set this up according to how your models are related, etc
#with the idea being to call @user.order_history and getting an array of previous orders
end
if @order.save
if @order.purchase
#adapt this pseudocode to suit your needs and
#according to how you've defined `@user.order_history
current_user.order_history = current_user.order_history + @order.line_items
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
基本上,将行\u项目推到其他地方,以便在销毁购物车之前记录它们 你能展示更多你的订单吗?为什么线_项目被销毁?或者您的订单在完成后被销毁?嗨,我已经更新了我的订单控制器。与大多数应用程序一样,行_项目将在成功付款后销毁。我现在正在尝试检索用户在付款后放置的产品。使用您选择的“假装”偏执的gem。我想知道gem有什么帮助,因为我目前正在考虑将所有数据转移到另一个模型。您知道怎么做吗?您好,我仍然无法检索,因为付款成功后,行项目将被销毁。您可以在行项目模型中添加列,状态为“待定”、“已处理”等。然后向关系中添加条件,例如:has\u many:processed\u products
,:至::行项目,条件:{“行项目.状态”:“已处理”},类名称:“产品”。它将允许您保存历史记录并跟踪所有内容。您好,我添加了一个名为order_products的附加表。但是,是否可以指导我如何将行_项传递到新模型中?谢谢你,谢谢你的帮助!但是,您是否知道有任何优雅的方法将此数据存储到另一个模型中?只需将@order.purchase下的代码替换为将数据存储在所述模型中的代码,而不是@user.Hi。在这里,我添加了一个名为order\u products的附加表。但是,是否可以指导我如何将行_项传递到新模型中?感谢取决于您的设置方式,如if@order.purchase current\u user.order\u products.new(@order.line\u items)
感谢您的帮助。但是,它为nil:NilClass声明了未定义的方法“new”