title: More Efficient Find Or Create Multiple Records In Rails
body: <p>I have an app that needs to send out user invitations for events.  When a user invites a friend (user) to an event, a new record connecting the user to the event is created if one doesn't already exist. My models consists of user, event, and events_user.</p>
  
  <pre><code>class Event
      def invite(user_id, *args)
          user_id.each do |u|
              e = EventsUser.find_or_create_by_event_id_and_user_id(self.id, u)
              e.save!
          end
      end
  end
  </code></pre>
  
  <p>Usage</p>
  
  <pre><code>Event.first.invite([1,2,3])
  </code></pre>
  
  <p>I don't think the above is the most efficient way to accomplish my task.  I envisioned a method like </p>
  
  <pre><code> Model.find_or_create_all_by_event_id_and_user_id
  </code></pre>
  
  <p>but one does not exist.</p>
  
  <p>Models without validations</p>
  
  <pre><code>class User 
    has_many :events_users 
    has_many :events 
  end 
  class EventsUser 
    belongs_to :events 
    belongs_to :users 
  end 
  class Event 
    has_many :events_users 
    has_many :users, :through =&gt; :events_users 
  end
  </code></pre>