Sunday, December 13, 2009

Re: [Rails] Finding non-empty has_many associations with a named_scope

ithank iam very rech no help  by

On Sun, Dec 13, 2009 at 2:10 AM, Ryan Waldron <rew@erebor.com> wrote:
If I have these classes:

class Customer < ActiveRecord::Base
  has_many :jobs
...
end

class Job < ActiveRecord::Base
  belongs_to :customer
...
end

can I construct a named scope that returns the equivalent collection (but hopefully more efficient) to this:

customers_with_jobs = Customer.all.reject{|t| t.jobs.empty?}

I don't care about the number of jobs a customer has, or the state any of those jobs are in; I just want a named scope that will give me the ones who have jobs associated with them, hopefully without the massive N+1 query problem that the reject{...} gets me.

It seems like this should be a relatively simple thing, but it has eluded me so far.

--

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.

--

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


Real Estate