class DeliveryDestination < ActiveRecord::Base  
  has_one :cart
  belongs_to :user

  def self.create_from_last(anonymous_id, user_id)
    dd = DeliveryDestination.where(["user_id is not null and user_id = ?", user_id]).order("id desc").first
    dd ||= DeliveryDestination.where(["anonymous_id_string is not null and anonymous_id_string = ?", anonymous_id]).order("id desc").first
    dd ||= DeliveryDestination.new({user_id: user_id, anonymous_id_string: anonymous_id })
    attributes = dd.as_json
    attributes.delete("id")
    result = DeliveryDestination.create!(attributes)
    return result
  end
end
