1

I want to convert the following into Rails Ruby code:

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.

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

Rob
  • 11
  • 4

1 Answers1

0
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.

Album.where(title: "Greatest Hits", artist: "The Beatles").count(:id)
messanjah
  • 8,977
  • 4
  • 27
  • 40
  • Thanks for the answer. Here is the link to my follow-up question: http://stackoverflow.com/questions/34113578/using-plucked-values-in-ruby-on-rails – Rob Dec 06 '15 at 03:11