title: How to get the id of newly created record in rails & associate the id with another model - Rails
body: <p>I'm building a rails app connected to a react app. The rails app has four models. For arguments sake here, let's pretend it has two models: a sender &amp; address. The sender belongs_to an address, and an address has_many senders. Therefore a sender has a foreign_key of address_id. When the sender registers for a new account, he creates an address as well. I'm trying to build something in my rails controller whereby when a new address is created it is associated with address_id column of the sender. I tried  building my own unique solution based on the solutions of <a href="https://stackoverflow.com/questions/42296201/how-to-get-the-id-of-the-newly-created-record-in-rails-way">this rails thread.</a> but I didn't have much success. I implemented the following in my address controller:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class AddressesController &lt; ActionController::API
      
      def show
          @address = Address.find(params[:id])
      end 
  
      def create 
          success = false
          address = Address.new(address_params)
          sender = @sender
          if address.save
              address_id = address.sender.build({address_id: address.id })
              success = true if !address_id.empty?
          else
              render json: { errors: address.errors.full_messages }, status: :bad_request
          end
          if success = true  
              render json: {status: 'address created successfully'}, status: :created 
          else  
              render('edit')
          end
      end 
  
      def address_params
          params.require(:address).permit(:streetname, :zipcode, :city, :country)
      end
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>And I'm getting this error in the terminal:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  
  NoMethodError (undefined method `sender' for #&lt;Address:0x00007f93b2576050&gt;
  Did you mean?  senders
                 senders=
                 send):
    
  app/controllers/addresses_controller.rb:21:in `create'</code></pre>
  </div>
  </div>
  </p>
  
  <p>So I placed an attr_accessor in my senders controller:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class SendersController &lt; ApplicationController
  attr_accessor :sender    
      def show
          @sender = Sender.find(params[:id])
      end 
  
      def create
          @sender = Sender.new(sender_params)
          if @sender.save 
            render json: {status: 'sender created successfully'}, status: :created
          else
            render json: { errors: sender.errors.full_messages }, status: :bad_request
          end
      end 
      
      def sender_params
          params.require(:sender).permit(:userid, :name, :email, :photourl, :address_id)
      end
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>I feel I'm over complicating things over here. So I really need help with this one. Thanks in Advance!</p>