How do I build a query in Ruby on Rails that joins on the max of a has_many relation only and includes a select filter on that relation?

前端 未结 7 1881
走了就别回头了
走了就别回头了 2021-01-06 12:56

I\'m struggling how to have Ruby on Rails do this query right... in short: to join on a has_many relation but only via the most recent record in that r

7条回答
  •  忘掉有多难
    2021-01-06 13:25

    +1 to @max's answer.

    An alternative though is to add a start_date and end_date attribute to Employment. To get active employees, you can do

    Employee
      .joins(:employments)
      .where('end_date is NULL OR ? BETWEEN start_date AND end_date', Date.today)
    

提交回复
热议问题