hzamani / acts_as_relation

Multi table Inheritance for rails
http://hzamani.github.com/acts_as_relation/
MIT License
180 stars 58 forks source link

N+1 Query detected #27

Closed bigardone closed 11 years ago

bigardone commented 11 years ago

Hi! How can I prevent N+1 queries? I have the following models

class Client < ActiveRecord::Base
  acts_as_superclass
end

class Buyer < ActiveRecord::Base
  acts_as :client, include: true
end

class Seller < ActiveRecord::Base
  acts_as :client, include: true
end

Every time I search for Buyers or Sellers, the bullet gem warns me about N+1 queries detected as it follows:

N+1 Query detected
  Provider => [:client]
  Add to your finder: :include => [:client]
N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack

I've tried with this param

class Provider < ActiveRecord::Base
  acts_as :client, include: :client
  ...

And even overriding the default_scope like this

class Provider < ActiveRecord::Base
  default_scope include: :client
  ...

But it keeps me giving me the same warning. What am I doing wrong? Thanks in advance!

hzamani commented 11 years ago

acts_as is a has_one relation, and there are no second order relations to include! If you have a model that has_many :buyer for example you can add include :as_client, ie has_many :buyers, include: :as_client. Or if you don't know whats the association name has_many :buyers, include: acts_as_association_name(:client)

hzamani commented 11 years ago

including supermodel is default behavior in 1.0.0 and 0.1.3