2

Suppose an Embedded 1-N model like the following :

class Band
  include Mongoid::Document
  field :group_name 

  embeds_many :albums
end

class Album
  include Mongoid::Document
  field :name
  field :sold

  embedded_in :band
end

How can I get a full list of Album names for each Band ?

I mean I have to get all bands at firsts, then for each band I'll get his name and I tried some variations of the following :

Band.all.each do |band|
  band.albums.all.each do |album|
    album.name
  end
end

but doesn't work, what am I missing ?

UPDATE

following abhas, it just works adding array :

album_array = []
Band.all.each do |band|
  band.albums.all.each do |album|
    album_array << album.name
  end
end

then

album_array.each{|a|a}

to get the list back.

Community
  • 1
  • 1
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109

1 Answers1

2

the loop you have gave is correct it works just fine, but you are not saving any value of album.name anywhere. Just save it in array or something and check the contents of the array. I think it will have all the names.

abhas
  • 5,193
  • 1
  • 32
  • 56