title: ActiveRecord has_one with a dynamic scope
body: <p>I have this</p>
  
  <pre class="lang-ruby prettyprint-override"><code>class Student
    has_many :assignments
  end
  
  class Assignment
    belongs_to :student
    belongs_to :book
  end
  
  class Book
    has_many :assignments
  end
  </code></pre>
  
  <p>To find the student's "Harry Potter" assignment, I am doing this</p>
  
  <pre class="lang-ruby prettyprint-override"><code>@student.assignments.find_by_book_id(Book.find_by_name('Harry Potter'))
  </code></pre>
  
  <p>Is there a way to make this into a has_one association for the <code>Student</code> model?</p>
  
  <p>Something like this (pseudo-code here)</p>
  
  <pre class="lang-ruby prettyprint-override"><code>class Student
    has_many :assignments
    has_one  :harry_potter_assigment, -&gt; { where book_id: Book.find_by_name('Harry Potter') }, class: 'Assignment'
  end
  </code></pre>
  
  <p>If not with a <code>has_one</code>, what is a better way?</p>