-1

Given a model named Album, I want to use the values I pluck. For starters, I want to try to print the values. How can I do this? Here is my current code:

my_arr = Album.where(title: "Greatest Hits", artist: "The Beatles").pluck(:publisher, :year_published)

puts my_arr[0][0].to_a

To make things even simpler, how would I retrieve any value at all, so that I could place it into a normal variable?

I'm using Rails 4 with SQLite3.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Rob
  • 11
  • 4
  • "No ouput" is alarmingly frequent. Add record pooulation code above what you tried so we know what records to ecpect. Also, versions of Rails and Ruby might be helpful. – D-side Dec 06 '15 at 08:40

4 Answers4

0

pluck already returns an array - you do not need to manipulate the output of

Album
  .where(title: "Greatest Hits", artist: "The Beatles")
  .pluck(:publisher, :year_published)

in any way - it is an array. In this case an array of 2-element arrays.

If you have an empty array - I guarantee you just do not have any records meeting the where conditions. It might be a result of a tiny typo in title's or artist's name whatsoever (which starts to be unrelated to question). If you are sure you have database objects that meet the filter - make sure to double check for typos, because your query is correct.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
-1

See if this helps:

my_arr = Album.where(title: "Greatest Hits", artist: "The Beatles").pluck(:publisher, :year_published)
my_arr.each do |album|
    puts "publisher #{album[0]}"
    puts "year_published #{album[1]}"
end
Raman
  • 1,221
  • 13
  • 20
  • please check `my_arr.count` and see if it is greater than `0` – Raman Dec 06 '15 at 05:03
  • I did puts my_arr.count. It comes back as an undefined method. Not sure why or what is wrong. – Rob Dec 06 '15 at 05:14
  • make sure you are not getting any exception on running the above code `Album.where(title: "Greatest Hits", artist: "The Beatles").pluck(:publisher, :year_published)` – Raman Dec 06 '15 at 05:49
  • Album.where(title: "Greatest Hits", artist: "The Beatles").count returns 0. I have no idea why, but the problem has to be regarding that. The seed file is working... – Rob Dec 06 '15 at 06:03
  • If you have artist in another model the query won't work like that – Raman Dec 06 '15 at 08:23
-1

You can use select to get an array of ActiveRecord models only with specified fields:

albums = Album.where(title: "Greatest Hits", artist: "The Beatles").select(:publisher, :year_published)
albums.each { |album| puts "publisher: #{album.publisher}, year: #{album.year_published}" }

In this case the code is more readable.

Jeiwan
  • 954
  • 6
  • 13
  • If it's Rails 3, you need to provide argument for select as an array: `select([:publisher, :year_published])` – Jeiwan Dec 06 '15 at 04:41
  • I found this:http://stackoverflow.com/questions/13512070/assigning-data-from-ruby-on-rails-table-to-a-javascript-variable So I tried this: albums = Album.where(title: "Greatest Hits", artist: "The Beatles").select(:publisher, :year_published)[0] It did not work... – Rob Dec 06 '15 at 05:08
  • You don't need to add [0] at the end, as it will select only one record, but `each` is only available on enumerables (e.g. array). You can just run my code as it is in Rails console and it will work. – Jeiwan Dec 06 '15 at 05:16
  • Jeiwan, I tried your code. I'm using Rails 4 with SQLite3, with ActiveRecord, but I still get no output. – Rob Dec 06 '15 at 05:22
  • Hmm... that's strange. Maybe there are no records in DB, try running `Album.where(title: "Greatest Hits", artist: "The Beatles").count` and see what it will return. – Jeiwan Dec 06 '15 at 05:27
  • Jeiwan, I have tried this, I have even created a new database in case the previous one had something corrupted. The count returns one, despite the fact that the seed file has been populated. For some reason, either the database is not being populated, or something is returning incorrectly – Rob Dec 06 '15 at 05:59
  • There was a typo in my code (`album.published` instead of `album.publisher`). Try it out. But still, there must be some output, at least errors. This is strange... – Jeiwan Dec 06 '15 at 06:18
-1

You can try with this :

    albums = Album.where(:title => "Greatest Hits", :artist => "The Beatles").take 
    albums.each do |album|
        puts "publisher #{album.publisher}, year published #{album.year_published}"
    end    

Hope this helped

Lorenzo Camaione
  • 505
  • 3
  • 15