Rails "where" method to search for parent attribute of child

I have a Rails application where I am trying to create a list of parent classes based on the date of their child classes.

right now i have:

orders = Order.where("order_reminders.date < ?", 1.month.from_now)

But I get an error message.

"no such column: order_reminders.date"

I am sure that my problem is related to my request, but I am not sure how to fix it.

+4
source share
2 answers

You need to use techniques such as references, joins, includesor eager_load, depending on what you need. The easiest way to determine what to choose is to go to the documentation / api and read them.

, order_reminders Order, joins, INNER JOIN, orders order_reminders, , , , -.

Order.joins(:order_reminders).where('order_reminders.date < ?', 1.month.from_now)
+7

order_reminders ?

order has_many order_reminders,

Order.joins(:order_reminders).where("order_reminders.date < ?", 1.month.from_now)
+1

Source: https://habr.com/ru/post/1610215/


All Articles