title: Ruby on Rails - save 3 values with one times
body: <p>I'm newer in Ruby on Rails. Now I want to save 3 images with one times in controller. I'm researched more in internet but I still not found answer and don't know how to do it?</p>
  
  <ul>
  <li>My database have some fields: id (int), product_id (int), link_photo (text)</li>
  </ul>
  
  <p>It's mean with one ID of product I can store 3 records of images (3 rows in database for one product)</p>
  
  <ul>
  <li>In view layer, I had input to upload files</li>
  </ul>
  
  <blockquote>
  <pre><code>        &lt;div class="form-group &lt;%= has_error?(@photo,"link_photo") %&gt;"&gt;
            &lt;label for="pictures"&gt;&lt;%= I18n.t("farms.pictures") %&gt;&lt;/label&gt;
            &lt;%= file_field :photo, :link_photo_first, class:"filestyle", accept:"image/png,image/jpeg" %&gt;&lt;br/&gt;
            &lt;%= file_field :photo, :link_photo_second, class:"filestyle", accept:"image/png,image/jpeg" %&gt;&lt;br/&gt;
            &lt;%= file_field :photo, :link_photo_third, class:"filestyle", accept:"image/png,image/jpeg" %&gt;
          &lt;/div&gt;
  </code></pre>
  </blockquote>
  
  <ul>
  <li><p>In controller:</p>
  
  <pre><code>  # Save the first photo
    if !params[:photo][:link_photo_first].nil?
      @photo.link_photo = params[:photo][:link_photo_first]
      @photo.save
    end
  
    # Save the second photo
    if !params[:photo][:link_photo_second].nil?
      @photo.link_photo = params[:photo][:link_photo_second]
      @photo.save
    end
  
    # Save the third photo
    if !params[:photo][:link_photo_third].nil?
      @photo.link_photo = params[:photo][:link_photo_third]
      @photo.save
    end
  </code></pre></li>
  </ul>
  
  <p>But if code like above it's can stored only one record at first.
  Please help me how to save it 3 images above. Thanks!</p>
  
  <p>======================</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Thanks everybody for reply. I knew the cause of it is: it's need renew again the @photo variable before save in next times.
  Maybe have many solutions to resolve it, but I edited like bellow and it's can run OK:</p>
  
  <p><strong>Fix like bellow: add @photo = Photo.create before save</strong></p>
  
  <blockquote>
    <h1>Save the first photo</h1>
  
  <pre><code>      if !params[:photo][:link_photo_first].nil?
          @photo = Photo.create
          @photo.product_id = @product.id
          @photo.link_photo = params[:photo][:link_photo_first]
          @photo.save
        end
  
        # Save the second photo
        if !params[:photo][:link_photo_second].nil?
          @photo = Photo.create
          @photo.product_id = @product.id
          @photo.link_photo = params[:photo][:link_photo_second]
          @photo.save
        end
  
        # Save the third photo
        if !params[:photo][:link_photo_third].nil?
          @photo = Photo.create
          @photo.product_id = @product.id
          @photo.link_photo = params[:photo][:link_photo_third]
          @photo.save
        end
  </code></pre>
  </blockquote>