I have the following "Query" using ActiveRecord:
Product.joins(product_attr_vals: [attr_val: [:attr]])
.joins(:product_model)
.where(product_models: {id: @product_model.id}, attrs: {id: attr.id})
.distinct
.count
I am not being able to understand why sometimes, in joins()
, rails accept the table name in plural and others in singular.
product_attr_vals:
(does not work if in singular -> product_attr_val)attr_val:
:attr
:product_model
Some info:
models/product_model_attr_val.rb
class ProductModelAttrVal < ApplicationRecord
validates :product_model_id, uniqueness: { scope: :attr_val_id }
belongs_to :product_model
belongs_to :attr_val
end
db/migrate/db_creation.rb
create_table :product_model_attr_vals do |t|
t.references :product_model, null: false, foreign_key: true
t.references :attr_val, null: false, foreign_key: true
t.timestamps
end