How to select record by distinct column in Rails3?

[亡魂溺海] 提交于 2020-01-06 18:55:13

问题


There is one model named Book, I want to get all books, but all results have distinct column named 'author', how to do this ? thanks!


回答1:


May be by:

Book.select("DISTINCT(author)")

for more read this: Active Record Query Interface


Edit

You can try: find_by_sql as

Book.find_by_sql("select * from books group by author")


来源:https://stackoverflow.com/questions/5701911/how-to-select-record-by-distinct-column-in-rails3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!