I've been looking for this for a while and can't seem to find an answer, although I think that it should be a pretty easy fix if you're more experienced with Rails than I am. I am trying to add multiple default_scope conditions to my Product model. My product model file is as follows:
class Product < ApplicationRecord
has_many :order_items
default_scope { where(active: true) }
validates :name, presence: true, length: { maximum: 300 }
PRICE_REGEXP = /\A\d{1,4}(\.\d{0,2})?\z/
validates :price, presence: true,
numericality: true,
format: { with: PRICE_REGEXP }
validates :description, presence: true, length: { maximum: 1000 },
allow_nil: true
end
I would like to also add
default_scope -> { order(created_at: desc) }
so that new products on the product index page are in the format of newest first. I get an syntax error message anytime I do something like:
default_scope -> { order(created_at: desc) }, { where(active: true) }
or
default_scope -> { order(created_at: desc), where(active: true) }
or
default_scope -> { order(created_at: desc) where(active: true) }
I know that this is probably just a syntax thing that I'm not understanding. If anyone could give me advice on how to fix this it would be greatly appreciated! Thanks!