title: Creating multiple has_many through associations from an array of IDs
body: <p>I have models <code>User</code>, <code>Photo</code> and <code>Favorite</code>, where <code>favorites</code> is a join table from <code>users</code> to <code>photos</code>, i.e.:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :favorites
    has_many :photos, through: `favorites`
  end
  
  class Photo &lt; ActiveRecord::Base
    has_many :favorites
    has_many :users, through: `favorites`
  end
  
  class Favorite &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :photo
  end
  </code></pre>
  
  <p>Say that <code>@user</code> is an instance of <code>User</code> and <code>photo_ids</code> is an array of primary keys of Photos. What's the fastest and/or most succinct way to add all of those photos to <code>@user.photos</code>?</p>
  
  <p>The best I can up with is:</p>
  
  <pre><code>@user.favorites.create( photo_ids.map { |id| {photo_id: id } } )
  </code></pre>
  
  <p>But this seems pretty verbose to me. Does Rails not have a better way of handling this?</p>
  
  <p>Other questions talk about creating multiple <code>has_many: through:</code> associations through a nested form but I'm trying to do this in a JSON API so there are no forms involved.</p>