title: Keep 10 records to create/update in Rails
body: <p>Using Rails 4. I am calling an API, which returns Shop and photos. When it returns photos, it could be up to 10 photos.</p>
  
  <p>Everytime I call the API, it will <code>find_or_initialize_by</code> a Shop, so that I always update the Shop details. At the same time, I also want to update the photos. There is no way to know whether the photos is the same or different, not even from the url. Different photo url could be same photos as created previously. There's no reference id, and I am not saving/downloading the photos in my server too.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>@shop = Shop.find_or_initialize_by(source_id: shop_params[:id])
  @shop.photos.build(shop_params[:photos])
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Let's say when I first creat Shop A, the API returns with 4 photos. I save all 4 photos. I call the API again for Shop A, the API returns with 10 photos. I want to save these 10 photos. I don't mind if the previous 4 are destroyed or updated. How do I make this work? The total photos should be 10 after saving.</p>
  
  <p>Then, I may call the API again, which returns 6 photos. Now I don't want to keep the previous 10 photos, but just the 6 photos.</p>
  
  <p>In my view, I will call <code>@shop.photos</code>, which is essentially the latest photos (up to 10).</p>
  
  <p><strong>Note</strong>
  The photos returned by API could be duplicate from the ones that I already have in my DB. That's the reason why I can't keep the previous photos.</p>