Can Rails' Active Record handle SQL aggregate queries?

前端 未结 3 1296
孤独总比滥情好
孤独总比滥情好 2021-02-02 02:33

Just started learning active record and am wondering how to best retrieve data from multiple tables where an SQL aggregate query is involved.

In the following example (f

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 03:24

    In the current version of AR (2.3) I think your only option is to use find_by_sql. Why? Your custom SELECT attributes. ActiveRecord allows :select and :include arguments to the find call. Unfortunately they can't be used together. If you specify both the :select will be ignored. In your example you need the select to limit your columns and to get your MAX function.

    I've heard there is a patch/hack to get them to work together, but I am not sure where it is.

    Peer

提交回复
热议问题