I'm trying to scope a has_many
relation of a model, by the model's instance variable.
I have a model like this:
class Category < ActiveRecord::Base
has_many :products, -> { where(product_type: @product_type) }
attr_accessor :product_type
Then I'd do something like this:
category = Category.find(id)
category.product_type = 'type'
category.products # expected output: SELECT * FROM `products` ... WHERE `product_type` = 'type'
In the snippet give, the problem is that the scope is trying to get @product_type
from the ActiveRecord::Relation
and not the model itself.
How do I make it work as intended?