sql - Rails 4: Multi-level joins with ActiveRecord -


i adding layer of complexity data model. previously: country had many stores, in turn had many products.

i introducing cities that: country has many cities have many stores have many products.

the data work contains stores may not have products (yet), cities may not have stores (yet) , countries may not have cities (yet).

i want query countries in find products. (i.e. can fill landing page countries makes sense user stuff.)

previously (with country > store > product), used this, , worked well:

country.joins(:stores => :products).group("countries.id").having("count(products.id)>0") 

but cannot head around additional layer (country > city > store > product). instance, doesn't work:

country.joins(:cities => :stores).joins(:stores => :products).group("countries.id").having("count(products.id)>0") 

...yielding error:

activerecord::configurationerror: association named 'stores' not found on country 

can help?

(i trying sql-agnostic possible, if can done activerecord methods way.)

country.joins(cities: [stores: :products])        .group('countries.id')        .having('count(products.id) > 0') 

because of joins nature having clause redundant:

country.joins(cities: [stores: :products]).uniq 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -