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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
no one? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 30 December 2011 10:54, Damien Knight <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
the latter. i would like to avoid: has_many :bs_auto, :class_name => ''B'', :conditions => ''auto'' AND the many enquiries. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.