Include and scopes

Hi,

I recently changed to rails 3 and the chainability for relations(i.e.
.where(xxx).order(xxx))

so here’s my question: can i use scopes in my includes to avoid
1+n-query problem?

i have:

class A < ActiveRecord::Base
has_many :bs
end

class B < ActiveRecord::Base
scope :auto, where(‘auto’) # auto is a boolean
end

now i would like to do this:
as = A.includes(:bs)
as.each do |a|
a.bs.auto #do something
end

but of course i get 1+n queries.
in rails 2 i would add this to my A class:
has_many :bs_auto, :class_name => ‘B’, :conditions => ‘auto’

now when i call:
as = A.includes(:bs_auto)
as.each do |a|
a.bs_auto #do something
end

i will get 2 queries.

can i do something like that with my rails 3-example and scopes? or do i
need the additional relation as well?

no one?

On 30 December 2011 10:54, Damien K. [email protected] wrote:

class A < ActiveRecord::Base
a.bs.auto #do something
end

i will get 2 queries.

can i do something like that with my rails 3-example and scopes? or do i
need the additional relation as well?

Are you saying you can no longer use the same code in Rails 3 or are
you asking whether there is a better way to achieve the same thing?

Colin

the latter.

i would like to avoid:
has_many :bs_auto, :class_name => ‘B’, :conditions => ‘auto’

AND

the many queries.