Active Record Rails的问题4

时间:2016-07-22 10:17:03

标签: ruby-on-rails activerecord

我正在尝试在OrdersController#new action中调用一些ActiveRecord方法。 我已经在Rails控制台中测试了代码,它按照我的预期方式工作。 但是在控制器动作中它不会产生相同的

  order has_many cart_items
  cart has_many cart_items 
  cart_items belong_to order cart_items
  belong_to cart 
  cart_items belong_to product
  product has_many cart_items

def new
  @order.new
  @order.cart_items = current_cart.cart_items
  @order.save

  current_cart.cart_items.destroy_all
end

现在current_cart是一个application_controller方法,用于检查当前用户是否有购物车。如果是这样,它会从数据库中提取该购物车,如果用户没有,那么它会为用户创建一个新购物车。我在这里要做的是当用户完成订单时我试图将cart_items从current_cart转移到订单然后清除购物车。

当我在rails控制台中执行此操作时,它会提供我想要的内容。使用current_cart中的cart_items订购,在我在购物车上运行destroy_all后,我有一个空的活动记录关联数组。

当我在我的控制器中测试它时,Order和Cart都返回一个空的活动关联数组。

这里发生了什么?

    #application controller method of finding current_users cart
    def current_cart
    # if user is logged in
    if current_user
      @user = current_user
      # checking user to see if account is confirmed and verified
      if @user.confirmed_at != nil

        # checking if user already has cart in cart database
        if Cart.find_by(users_id: @user.id) != nil
          # find a row in the database where users_id: equal to @user.id
          # where clause does not work here
          cart = Cart.find_by(users_id: @user.id)
          session[:cart_id] = cart.id
          cart.save
          #establish Cart session cart for user
          Cart.find(session[:cart_id])
        else
          # create a new Cart Object for user.assign current_user's id to cart object
          cart = Cart.new
          cart.users_id = @user.id
          # save it to get cart id assign session[:cart_id] == cart.id
          cart.save
          session[:cart_id] = cart.id
        end
      end
    end
  end



class CartItemsController < ApplicationController
  before_action :set_cart_item, only: [:show, :edit, :update, :destroy]
# scope for most_recent and subtotal
# find out if rails sorts on update column cuz this is annoying.


  def create
    # grabbing cart from application controller current_cart method
    @cart = current_cart
    # session[:cart_id] = @cart.id
    # individual product items get added to cart item and added to cart and saved
    @cart_item = @cart.cart_items.build(cart_item_params)
    @cart.save
  end

  def update
    @cart = current_cart
    # finding cart_items by cart_id
    @cart_item = @cart.cart_items.find(params[:id])
    # @cart_items.order(:id)
    @cart_item.update_attributes(cart_item_params)
    @cart_items = @cart.cart_items.order(:id)
    # redirect 'cart_show_path'
    @cart.save
  end

  def destroy
    @cart = current_cart
    @cart_item = @cart.cart_items.find(params[:id])
    @cart_item.destroy
    @cart_items = @cart.cart_items
    @cart.save
  end

  private
    def set_cart_item
      @cart_item = CartItem.find(params[:id])
    end

    def cart_item_params
      params.require(:cart_item).permit(:cart_id, :product_id, :unit_price, :quantity, :total_price)
    end
end

2 个答案:

答案 0 :(得分:2)

当您说要从Safari转移cart_items时,您传递的是对象,而不是创建新的current_cart(这意味着数据库ID相同),当您执行cart_items它是从数据库中删除它们。见ActiveRecord::Relation#destroy_all

对于你的用例,如果你只做

就足够了
current_cart.cart_items.destroy_all

答案 1 :(得分:0)

好吧,想通了。

我正在使用@ order.save,它没有处理错误消息!!!!!!!! 在@ order.save之后!它给了我另一个模型的验证错误。 我评论说它有用了。

我迭代了current.cart_items,并将cart_id分配给nil,将order_id分配给@ order.id,基本上清除了购物车并且&#34;转移&#34;物品结束。

虽然我无法找到使用destroy_all的方法。我认为这不可能像@Sri说的那样。

非常感谢!

所以结束代码是这样的:

@order = Order.create!(users_id: current_cart.users_id)

current_cart.cart_items.each do |item|
  item.order_id = @order.id
  item.save!
end

if @order.save
  current_cart.cart_items.each do |item|
  item.cart_id = nil
  item.save!
end