Ruby on Rails Friday, December 30, 2011

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?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment