ruby on rails - Values from Array with Mongoid -


i'm doing relation between users , want access values embedded_in document mongoid generates this, need friendlist:

>>   current_user  => #<user _id: bson::objectid('57a4df6927d8754bd68aaade'), contacts: [{"_id"=>bson::objectid('57a4df7a27d8754bd68aaadf'), "created_at"=>fri, 05 aug 2016 18:48:26 utc +00:00, "friendlist"=>[bson::objectid('57a4df6927d8754bd68aaade'), bson::objectid('5790f58a27d8757b4ff547fd'), bson::objectid('5790f43727d87576358ae575')], "owner"=>"57a4df6927d8754bd68aaade", "updated_at"=>fri, 05 aug 2016 18:48:26 utc +00:00}], created_at: fri, 05 aug 2016 18:48:09 utc +00:00, email: "testauth@myapp.com", havefiles: false, image: nil, location: nil, updated_at: mon, 08 aug 2016 15:39:31 utc +00:00, username: "testauth"> 

here line need:

"friendlist"=>[bson::objectid('57a4df6927d8754bd68aaade'), bson::objectid('5790f58a27d8757b4ff547fd'), bson::objectid('5790f43727d87576358ae575')] 

my code looks this

class maincontroller < applicationcontroller   def index     @listofusers = user.all     user.find_by(id: current_user.id).where(contacts: [friendlist]) |listofcontacts|       puts "***list of contacts***"       puts listofcontacts     end   end end 

models

user

## customable ;)   field :username, :type => string   field :havefiles, :type => boolean, default: false   field :location, :type => string   field :image, :type => string   embeds_many :contacts    embeds_many :favlists   # validate presence of type username   validates_presence_of :username 

contact

class contact   include mongoid::document   include mongoid::timestamps   field :owner, :type => string   field :friendlist, :type => array   embedded_in :users end 

i guess want this:

class maincontroller < applicationcontroller   def index     @listofusers = user.all     puts "***list of contacts***"     user.in(contacts: current_user.id) |user|       puts user     end   end end 

this work if contacts array of bson::objectids. correct me if wrong.


Comments