Re: [Rails] include and scopes
On 30 December 2011 10:54, Damien Knight <lists@ruby-forum.com> 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@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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home