title: Ruby on Rails where query with relations
body: <p>I am trying to use where query with relationships.</p>
  
  <p>How can I query using where with relations in this case?</p>
  
  <p>This is model</p>
  
  <pre><code>User
    has_many :projects
    has_many :reasons, through: :projects
  
  Project
    belongs_to :user
    has_many :reasons
  
  Reasons
    belongs_to :project
  </code></pre>
  
  <p>This is the codes which doesn't work</p>
  
  <pre><code>  # GET /reasons
    def index
      reasons     = current_user.reasons
      updated_at  = params[:updated_at]
  
      # Filter with updated_at for reloading from mobile app
      if updated_at.present?
  
  
  
        # This one doesn't work!!!!!!!!!!!!      
        reasons = reasons.includes(:projects).where("updated_at &gt; ?", Time.at(updated_at.to_i))
  
  
  
      # Get all non deleted objects when logging in from mobile app
      else
        reasons = reasons.where(deleted: false)
      end
  
      render json: reasons
    end  
  </code></pre>
  
  <p>---Update---</p>
  
  <p>This is correct thanks to @AmitA.</p>
  
  <pre><code>reasons = reasons.joins(:project).where("projects.updated_at &gt; ?", Time.at(updated_at.to_i))
  </code></pre>