Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将SQL语句转换为Rails ActiveRecord_Sql_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

将SQL语句转换为Rails ActiveRecord

将SQL语句转换为Rails ActiveRecord,sql,ruby-on-rails,ruby,activerecord,Sql,Ruby On Rails,Ruby,Activerecord,我想将以下内容转换为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'; 首先,我读过Ruby中的Purch方法,但是我不知道如何进行Purch,因为这里有很多限制,不仅仅是标题,还有艺术家 对于第二个问题

我想将以下内容转换为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';
首先,我读过Ruby中的Purch方法,但是我不知道如何进行Purch,因为这里有很多限制,不仅仅是标题,还有艺术家

对于第二个问题,我只想计算一下标题和艺术家相同的次数

请帮忙? 我最好的消息来源:

假设一个名为Album的模型

Album.where(title: "Greatest Hits", artist: "The Beatles")
第二次

SELECT COUNT(ID) FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
使用
count
方法

Album.where(title: "Greatest Hits", artist: "The Beatles").count(:id)
假设一个名为Album的模型

Album.where(title: "Greatest Hits", artist: "The Beatles")
第二次

SELECT COUNT(ID) FROM ALBUMS
WHERE TITLE='Greatest Hits'
AND ARTIST='The Beatles';
使用
count
方法

Album.where(title: "Greatest Hits", artist: "The Beatles").count(:id)

谢谢你的回答。下面是我后续问题的链接:谢谢你的回答。以下是我后续问题的链接: