I use association to map between my Store model and my Product model, using Store has_many :products and Product belongs_to :store. In this case, I can simply get store info and all products of the store using this simple code:
@store = Store.find_by_id params[:store_id]
But, I recently added a status field to products. This status is set by default to ON and when the user wants to destroy a product, this status is set to OFF instead of deleting the product on database.
But in this case, in the previous code, I get all products (whatever the product status is).
Question: How can I get store info AND ONLY products with :status => ‘ON’?
Thanks in advance.
You can use default scope for your model
update:
You can skip default scope like that:
If you don’t want to use default scope you can try this: