13 lines
595 B
Ruby
13 lines
595 B
Ruby
class Cart < ActiveRecord::Base
|
|
has_many :item_in_carts, -> { order "created_at" }
|
|
belongs_to :delivery_destination
|
|
|
|
def self.find_or_create(anonymous_id, user_id)
|
|
cart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
safe_user_id = (user_id > 0) ? user_id : nil
|
|
cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
|
cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
return cart
|
|
end
|
|
end |