[英]Convert SQL Statement to Rails ActiveRecord
I want to convert the following into Rails Ruby code: 我想将以下代码转换为Rails Ruby代码:
SELECT * FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
SELECT COUNT(ID) FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
For the first, I've read about the pluck method in Ruby, but I don't know how to pluck given multiple limitations, in this case, not just title, but also artist. 首先,我已经了解了Ruby中的pluck方法,但由于多种限制,在这种情况下,不仅标题,而且还有艺术家,我都不知道如何采摘。
For the second, I want to just count how many occurrences of title and artist being the same. 第二,我只想计算标题和艺术家相同的出现次数。
Please help? 请帮忙? My best source: http://apidock.com/rails/ActiveRecord/Calculations/pluck 我最好的资料来源: http : //apidock.com/rails/ActiveRecord/Calculations/pluck
SELECT * FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
Assuming a model named Album 假设一个名为专辑的模型
Album.where(title: "Greatest Hits", artist: "The Beatles")
For the second 对于第二
SELECT COUNT(ID) FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
Use the count
method. 使用count
方法。
Album.where(title: "Greatest Hits", artist: "The Beatles").count(:id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.