title: Use scope of a belongs_to association to scope in the first model - Ruby on Rails
body: <h1>Problem</h1>
  
  <p>Lets say I have two Models with a has_many-belongs_to relation. The has_many has a scope defined and an integer attribute named grade.</p>
  
  <pre><code>class Parent &lt; ApplicationRecord
    has_many :children
    scope :great, -&gt; (min_grade) {where("grade &gt; :grade", grade: min_grade)}
  end
  
  class Child &lt; ApplicationRecord
    belongs_to :parent
  end
  </code></pre>
  
  <p>I want to create an scope on the child model, that uses the scope of the parent model. </p>
  
  <p>Is there anyhow so that I can use the definition of scope on Parent?</p>
  
  <h1>Current solution</h1>
  
  <p>The way I'm doing it right now is </p>
  
  <pre><code>class Child &lt; ApplicationRecord
    belongs_to :parent
    scope :wit_great_parent, -&gt; (min_grade) {
          join(:parent).where("grade &gt; :grade", grade: min_grade)}
  end
  </code></pre>
  
  <p>However, I'm copying the where clause in both places.</p>
  
  <h1>Question</h1>
  
  <p>Is there anyhow to call the Parent scope from the child model?</p>