Разделите предметы в корзине на разные кассы - PullRequest
0 голосов
/ 22 января 2020

Привет, я настроил корзину для покупок в своем приложении, используя эту замечательную статью https://web-crunch.com/ruby-on-rails-ecommerce-music-shop/

Моя проблема в том, что, поскольку у разных товаров разные продавцы, они нужны мне в корзина должна быть разделена на разные кассы. Мне удалось создать группу, основываясь на продавце:

@groups = @cart.line_items.group_by {|line_item| line_item.listing.user_id}.values

, но я не уверен, как включить эти отдельные группы в свои собственные кассы. Любая помощь будет прекрасна!

class LineItem < ApplicationRecord
  belongs_to :listing
  belongs_to :cart

  def total_price
    listing.price_cents.to_i * quantity.to_i
  end
end

class Checkout < ApplicationRecord
  belongs_to :user
  has_many :line_items, dependent: :destroy
end


class Cart < ApplicationRecord
  has_many :line_items, dependent: :destroy

  def add_listing(listing)
    current_item = line_items.find_by(listing_id: listing.id)

    if current_item
      current_item.increment(:quantity)
    else
      current_item = line_items.build(listing_id: listing.id)
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

end

Спасибо

...