title: How to get the id of the newly created record in rails way?
body: <p>I have 2 models, suppose <strong>model1</strong> and <strong>model2</strong>. <strong>model1</strong> has many <strong>model2</strong>. <strong>model2</strong> <strong>belongs</strong> to <strong>model1</strong>. Save <strong>model1</strong> and <strong>model2</strong> at the same time.</p>
  
  <pre><code>class Model1 &lt; ActiveRecord::Base
    has_many :model2s
  end
  
  class Model2 &lt; ActiveRecord::Base
    belongs_to :model1
  end
  
  def create
     @mod1 = Model1.new(model1_params)
     id = Model1.last.id + 1
     @mod2 = Model2.new(model1_id: id)
     if @mod1.save &amp;&amp; @mod2.save 
      redirect root_path
     else
      render 'edit'
     end
  end
  </code></pre>
  
  <p>That solution is ok until I delete the last record of the model1. How can I get the last record of model1 before it created.</p>