mardi 9 octobre 2018

Rails 5, scope with agregation

I have two models, product and image. Product has many images. Each iemage has position field with number.

 Product
 id | name
 1  | Car

Image 
id| product_id| file_name | position
1 | 1         | asdad.jpg | 1
1 | 1         | asda.jpg  | 2

How to create scope to one image with trhe lowest position number?

scope :main_image, -> { ??? }

And than:

@product.main_image.image.url

Regards Sssebaa

Aucun commentaire:

Enregistrer un commentaire