Reputation: 1
I'm new at Rails... Is there a better way to refactor this code:
def get_product_price_minimum
Product.minimum(:price).to_i
end
def get_product_price_maximum
Product.maximum(:price).to_i
end
Upvotes: 0
Views: 83
Reputation: 33420
You can define something like "prices" (a vague method name, to avoid using get_ or set_ prefixes) to expect an argument, which would be the maximum or minimum for which to query your model:
def prices(what)
Product.public_send(what, :price).to_i
end
Then you can use it by passing the minimum or maximum as a symbol or a string.
Upvotes: 2
Reputation:
I'm not sure your function works but you can try (if price is column and price is a int not a string)
def get_product_price_minimum
Product.order('price').last
end
Upvotes: 0