title: Rails. How to update a bunch of entries inside a model
body: <p>I have a situation where users can apply to a job (called Job Application). When the employer of the job accepts/approves the user for the job, the model should automatically check if the user have another job within the same time.</p>
  
  <p>1) If there is, it will raise an error message saying the user already have a job. Which is done by the self.errors.add</p>
  
  <p>2) Should set the status of all other application around that time for the user to "User Already Booked". </p>
  
  <p>I was able to do 1) without any problems, however when I do 2) the SQL transaction keep giving me "rollback". I first got all the entries I want to update, then I tried using update_all method to update them. However the database won't commit the transaction and keep giving me a rollback.</p>
  
  <p>The following is my code. Can anyone tell me how to save records(not just 'self' but other entries) in a model. Or at least how to debug that rollback message.</p>
  
  <pre><code>#Inside my Job Application Model
  #Run the custom validation I made
  validate :does_user_have_job
  
  private
  
  #check if User have another job_application that overlaps with the current one
  def does_user_have_job
    #pre define variables for easier understanding
    job = self.job
    id = job.id
    start_time = job.start_time
    end_time = job.end_time
  
    #This part is a query to the database to find whether there is overlap.
    #Please ignore this as this is not important.
    overlaps = self.user.job_applications.joins(:job).where.not(job_id: id).where(approved: true).where("(jobs.start_time &gt;= ? AND jobs.start_time &lt;= ?) OR (jobs.end_time &lt;= ? AND jobs.end_time &gt;= ?)", start_time, end_time, end_time, start_time)
  
    #if there are overlaps, then set the status of those overlaps to "User Already Booked"
    if not overlaps.empty?
      #Find the entries that I want to set status to
      #I am picking out these entries for testing purposes
      #Do not ask my why I am using application where approved = false 
      job_applications = self.user.job_applications.where(approved: false)
  
      #This is the first method I used but the transaction
      #got rollback therefore I used the second method
      #job_applications.update_all(status: "User Already Booked")
  
      #This is the second method I used but the transaction
      #still got rollback and error message was empty
      job_applications.each do |job_application|
        job_application.assign_attributes(status: "User Already Booked")
        job_application.save(validate: false)
        puts job_application.errors.messages
      end  #scroll down for more vvvvvv
  
      #Third method I tried as suggested by "steve klein"
      #Still got rollback
      job_applications.each do |job_application|
        job_application.update_attribute(:status, "User Already Booked")
      end
  
      #After setting the status of other entries I raise an error
      self.errors.add(:job_application, "User is already booked")
    end
  end
  </code></pre>
  
  <p>Error message I got upon inspecting</p>
  
  <pre><code>SQL (0.1ms)  UPDATE "job_applications" SET "status" = ?, "updated_at" = ? WHERE "job_applications"."id" = ?  [["status", "Already Have Job"], ["updated_at", "2015-06-24 14:06:18.480217"], ["id", 72]]
  &lt;&lt;&lt; inspecting error
  #&lt;ActiveModel::Errors:0x007f9f3dc94378 @base=#&lt;JobApplication id: 72, job_id: 70, user_id: 1, master_id: 1, status: "Already Have Job", user_reviewed: false, master_reviewed: false, started_user: false, ended_user: false, created_at: "2015-06-23 17:14:52", updated_at: "2015-06-24 14:06:18", approved: false&gt;, @messages={}&gt;
  (1.0ms)  rollback transaction
  </code></pre>