rails/rails — Issue #3836: Has_many :through _ids not passing conditions to SQL (reopen #1005)
Author: wireframe
Created: 2011-12-02 21:46:55+00:00
State: open

Opening a new issue which is still in rails 3.0.x releases.
original lighthouse ticket: http://rails.lighthouseapp.com/projects/8994/tickets/6680
original github issue: #1005

Hi everyone,

First off, I love Rails. It's a fantastic framework.

So on to this possible bug...

Say I have a Book model with many authors through an AuthorAssignment model. The AuthorAssignment model also has an additional boolean column named featured.

class Book < ActiveRecord::Base
has_many :author_assignments, :dependent => :destroy
has_many :featured_authors, :through => :author_assignments, :conditions => "`author_assignments`.featured = 1"
end
Here is my problem:

When I call:

@book.featured_author_ids
The expected array of ids is incorrect. I get an array of all the author ids, not just the featured authors. The SQL query doesn't include the condition that 'author_assignments'.featured must be true.

SELECT `author_assignments`.author_id FROM `author_assignments` WHERE (`author_assignments`.book_id = 4)
On the other hand, when I fetch the @book.featured_authors records:

@book.featured_authors
The 'author_assignments'.featured condition is included in the SQL finder and the expected result for records is correct.

Thanks,
Chris