title: How can I get an id of an object that is not saved in db yet?
body: <p>I have a method in my <code>Cart</code> model:</p>
  
  <pre><code>def add_product_with_variant(product_id, variant_id)
    current_item = line_items.find_by(product_id: product_id)
    if current_item
      current_item.amount += 1
      line_item_variant = LineItemVariant.new(variant_id: variant_id, line_item_id: current_item.id)
      line_item_variant.save
    else
      current_item = line_items.build(product_id: product_id)
      line_item_variant = LineItemVariant.new(variant_id: variant_id, line_item_id: current_item.id)
      line_item_variant.save!
    end
  end
  </code></pre>
  
  <p>In the <code>else</code> statement I use <code>LineItemvariant.new(variant_id: variant_id, line_item_id: current_item.id)</code> to assign a <code>variant</code> to a <code>line_item</code>. But in this case, I can't save <code>LineItemVariant</code>, because the <code>line_item</code> does not exist yet, and I can't use this part of code: <code>line_item_id: current_item.id</code>. Is there any solution to get the id, or there is some easier option to solve this issue? Thanks ahead.</p>