Virtual columns in a model

Is it possible to add a virtual column to a model (not virtual attribute!)?

I have following situation: A Product(id, name) has many ProductVariant(id, price, offer_price, product_id,...)

When I select all products I want to have the minimum product price from all ProductVariants in the products result.

@products = Product.with_min_price.order('min_price ASC')

I calculate the minimum price in a sql query (with_min_price) and want to add this min_price value to each Product in my @products result.

Answers


This could just be a method in your Product class assuming your Product has_many ProductVarients

#Product Model

def min_product_price
  product_variants.map(&:price).min
end

Need Your Help

Python SQLite3 Problem with?

python sql sqlite3

Im currently learning SQLite3 with Python. I'm looking at the manual, and it tells me to do something like the following:

using image in if statement

c# image if-statement

how can i use image in if statement?