title: How to access the record attributes within Rails .where()
body: <p>How would one go about accessing an object's attributes within <code>.where()</code>?</p>
  
  <p>I am trying to run this line of code:</p>
  
  <pre><code>Task.where("(created_at &lt;= ?", Time.now - &lt;deadline&gt;).update_all(expired: true)
  </code></pre>
  
  <p>The idea being that any task that was created_at a time older than the set deadline (Time-now - deadline) will have it's expire attribute set to true.</p>
  
  <p>The problem is: <code>deadline</code> is an attribute of Task and each task will have it's own deadline. So I need <code>.where()</code> to check the following logic: <em>for any given Task object, if created_at is less than Time.now minus (here is the part I am stuck on)  than set it's expired attribute to true</em></p>
  
  <p>As pseudo code: </p>
  
  <pre><code>Task.where( Task's created_at attr is &lt;= to the current time - Tasks's deadline attr ).update_all( set Task's expired attr to true )`
  </code></pre>
  
  <h3>In summary:</h3>
  
  <p>I need to access an object's attribute as part of the logic within <code>.where()</code>.</p>
  
  <p><em>Some of my other tries:</em></p>
  
  <ul>
  <li><p><code>Task.where("(created_at &lt;= ?", Time.now - :deadline).update_all(expired: true)</code> TypeError: can't convert Symbol into an exact number</p></li>
  <li><p><code>Task.where("(created_at &lt;= ?", Time.now - self.deadline).update_all(expired: true)</code> NoMethodError: undefined method `deadline' for main:Object</p></li>
  <li><p><code>Task.where("(created_at &lt;= ?", Time.now - params[:deadline]).update_all(expired: true)</code> NameError: undefined local variable or method `params' for main:Object</p></li>
  </ul>
  
  <p>EDIT:</p>
  
  <pre><code>  create_table "tasks", force: :cascade do |t|
      t.string   "name"
      t.integer  "user_id"
      t.datetime "created_at",                  null: false
      t.datetime "updated_at",                  null: false
      t.boolean  "expired",    default: false
      t.integer  "deadline",   default: 604800
    end
  </code></pre>
  
  <p>I am running rails in dev mode, so I think the DB is SQL,
  The deadline attribute should be an integer (created with 7.days or an actual value)</p>