This project is archived and is in readonly mode.

#1845 ✓resolved
Eduardo

Finder methods ignore :include in has_many :through

Reported by Eduardo | February 2nd, 2009 @ 04:07 PM

If an has_many :through association has an :include and :conditions in it, calling a finder method on it will fail if the conditions refer to the included table, because the :include is ignored.

With this simple class definitions:


class Author < ActiveRecord::Base
has_many :authorships
has_many :books_with_preview, :source => :book,
         :through => :authorships, :include => :chapters,
         :conditions => ['chapters.preview = 1']
end

class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end

class Book < ActiveRecord::Base
has_many :authorship
has_many :chapters
end

class Chapter < ActiveRecord::Base
belongs_to :book
end

you can call


author.books_with_preview

but the following call will fail:


author.books_with_preview.find(:all)

I'm using rails 2.1.0 but I've been told that it's the same in rails 2.2.2. I tested mysql and sqlite, and I get the same error:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'chapters.preview' in 'where clause': SELECT books.* FROM books INNER JOIN authorships ON books.id = authorships.book_id WHERE ((authorships.author_id = 1) AND ((chapters.preview = 1)))

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages