title: Rails - How to use custom attribute in where?
body: <p>I have Order model in which I have datetime column <em>start</em> and int columns <em>arriving_dur</em>, <em>drop_off_dur</em>, etc.. which are durations in seconds from <em>start</em></p>
  
  <p>Then in my model I have</p>
  
  <pre><code>class Order &lt; ApplicationRecord
    def finish_time
      self.start + self.arriving_duration + self.drop_off_duration
    end
    # other  def something_time ... end
  end
  </code></pre>
  
  <p>I want to be able to do this:</p>
  
  <pre><code>Order.where(finish_time: Time.now..(Time.now+2.hours) )
  </code></pre>
  
  <p>But of course I can't, because there's no such column finish_time. How can I achieve such result?</p>
  
  <p>I've read 4 possible solutions on SA:</p>
  
  <ul>
  <li>eager load all orders and select it with filter - that would not work well if there were more orders</li>
  <li>have parametrized scope for each time I need but that means soo much code duplication</li>
  <li>have sql function for each time and bind it to model with select() - it's just pain</li>
  <li>somehow use <a href="http://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute</a> ? But I have no idea how to use it for my case or whether it even solves the problem I have.</li>
  </ul>
  
  <p>Do you have any idea or some 'best practice' how to solve this?
  Thanks!</p>