I’m looking for someone to confirm the following as a bug in Rails
1.0 before I post it to Trac.
If I use eager loading on an association that has conditions defined,
the conditions are ignored.
The following example illustrates the problem. I have two tables:
create_table “blogs”, :force => true do |t|
t.column “name”, :string
create_table “posts”, :force => true do |t|
t.column “blog_id”, :integer
t.column “subject”, :string
t.column “published”, :boolean
The model for blogs looks like this:
class Blog < ActiveRecord::Base
has_many :published_posts, :class_name => ‘Post’, :foreign_key
=> ‘blog_id’, :conditions => ‘published = 1’
Now add some sample data for blogs:
and for posts:
The problem is that:
Blog.find(:all, :include => :published_posts).first.published_posts
returns all the posts for the blog, not just the published ones. The
SQL generated looks like this:
id AS t1_r0, posts.
blog_id AS t1_r1,
id AS t0_r0, posts.
subject AS t1_r2,
name AS t0_r1, posts.
content AS t1_r3,
published AS t1_r4
FROM blogs LEFT OUTER JOIN posts ON posts.blog_id = blogs.id
The following, on the other hand, returns just the posts marked as
If anyone wants a simple sample project to test this, let me know and
I’ll tar up my example.