Hi,
I have a Model of Users and a Model of Products. My Model Products belongs_to my Model User and a User has_many Products.
My question is how can i search 1 or multiple products matching a user attribute?
exemple: Product.where(price: 10) for user.where(id: 2)
What is the solution for nested model search, i'm a bit lost. Many Thanks
Since products belongs to user (and user has_many products), you can query off of the relation:
user = User.find(2)
products = user.products.where(price: 10)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.