title: What's the better practice to reuse the where query
body: <p>How to reuse the same where queries in the 2 methods ?</p>
  
  <pre><code>.where(sony_alarm_tests: {ip: sony_alarm_test.ip})
  .where(sony_alarm_tests: {round: sony_alarm_test.round})
  .where(sony_alarm_tests: {firmware_version: sony_alarm_test.firmware_version})
  </code></pre>
  
  <p>The trouble for me is the queries depends on <code>.joins(:sony_alarm_test)</code>.</p>
  
  <p>I have no idea ? Thanks</p>
  
  <pre><code>  def previous(event_name=self.name)
      SonyAlarmLog.joins(:sony_alarm_test)
      .where{ utc_time.lt (my{self.utc_time} - TIME_CORRECTION) }
      .where{ name =~ event_name }
      .where(sony_alarm_tests: {ip: sony_alarm_test.ip})
      .where(sony_alarm_tests: {round: sony_alarm_test.round})
      .where(sony_alarm_tests: {firmware_version: sony_alarm_test.firmware_version})
    end
  
    def following(event_name=self.name)    
      SonyAlarmLog.joins(:sony_alarm_test)
      .where{ name =~ event_name }
      .where{ utc_time.gt (my{utc_time} + TIME_CORRECTION) }
      .where(sony_alarm_tests: {ip: sony_alarm_test.ip})
      .where(sony_alarm_tests: {round: sony_alarm_test.round})
      .where(sony_alarm_tests: {firmware_version: sony_alarm_test.firmware_version})
    end
  </code></pre>