rails/rails — Issue #3912: Enable has_many to accept arguments for dynamic conditions when eager loading
Author: Axsuul
Created: 2011-12-09 00:03:37+00:00
State: open

I wish to eager load associations based on dynamic conditions. Currently what I have is

```ruby
class Article
has_many :comments, :conditions => Proc.new { { :country => Comment.country } }
end

# Possible race condition since class variables are shared across all user requests
Comment.country = "US"

# Eager loads comments from the US
Article.includes(:comments).all
```

It would be nice if there was a way to insert arguments into the includes so I don't have to set a global flag. Something like

```ruby
class Article
has_many :comments, :conditions => Proc.new { |country| { :country => country } }
end

Article.includes(:comments).with(:comments => "US")
```