I am facing some problem after trying to upgrade from Rails version 4.2.0 to 5.2.8. Please let me know is there any problem, or why it happened with AR?
class Product < ApplicationRecord
use_single_schema_multi_tenant
has_many :product_categories_products, :dependent => :destroy
has_many :categories, through: :product_categories_products, class_name: "ProductCategory"
belongs_to :site
class << self
def active_products(site, store = true)
products = store ? self.not_flagged : self.unscoped
products.eager_load(:categories).where(site: site).order(updated_at: :desc).active.friendly
end
end
end
class ProductCategory < ApplicationRecord
use_single_schema_multi_tenant
belongs_to :site, inverse_of: :site_product_categories
has_many :product_categories_products
has_many :products, through: :product_categories_products
end
class ProductCategoriesProduct < ApplicationRecord
belongs_to :product
belongs_to :category, foreign_key: :product_category_id, class_name: "ProductCategory"
end
@products = Product.active_products(@site, @shopfront)
@products = @products.where(product_categories: {id: @category.id})
From the above Rails 4.2.0 - It is able to generate the SQL query like below.
Select * FROM "products" p
LEFT OUTER JOIN "product_categories_products" pcp ON pcp.”tenant_id" = 2 AND pcp.”product_id" = p.”id"
LEFT OUTER JOIN "product_categories" pc ON pc.”tenant_id" = 2 AND pc.”id" = pcp.”product_category_id" AND pcp.”tenant_id" = p.”tenant_id" WHERE p.”tenant_id" = 2 and products.site_id = 49285 and products.status = 1 and pc.id = 151
From the above Rails 5.2.8 - It is able to generate the SQL query like below.
category ID is not appending to the active_products query.
select * FROM "products"
LEFT OUTER JOIN "product_categories_products" ON "product_categories_products"."tenant_id" = 2 AND "product_categories_products"."product_id" = "products"."id"
LEFT OUTER JOIN "product_categories" ON "product_categories"."tenant_id" = 2 AND "product_categories"."id" = "product_categories_products"."product_category_id" AND "product_categories_products"."tenant_id" = "products"."tenant_id"
WHERE "products"."tenant_id" = 2