0

I have 2 Models, Kid and friend_list. To the the kid I use:

k = Kid.where(email: "[email protected]").first

Then, to get the friend list I type:

k.friend_list

and I get:

[#<FriendList _id: 5305cb6485216d2689004785, _type: nil, name: "Friends", members: ["5374a1f320db90054c0000ea", "537c63ea20db9040d2000332"], kid_id: BSON::ObjectId('5305cb6285216d2689004742'), teacher_id: nil>]

But I only need the "members".

I tried

k.friend_list.members, but I get

NoMethodError: undefined method `members' for
#<Array:0x007fcf4b013138> from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484@blabloo/gems/mongoid-2.8.1/lib/mongoid/criteria.rb:387:in
`method_missing'

How can I get only the members array??

Thanks in advance.

2
  • Friend_list returns a list of objects, do you want members of each element in a collection or just the members of first element? Commented Jun 17, 2014 at 10:24
  • Why are you getting array as result? does Kid has belongs_to: friend_list ? Commented Jun 17, 2014 at 10:32

1 Answer 1

1

It seems that friend_list returns an Array of FriendList.

You can create a new list composed of the values of the members getter using map:

k.friend_list.map(&:members)
# => [["5374a1f320db90054c0000ea", "537c63ea20db9040d2000332"]]

Or, alternatively, if you only meant to have a single FriendList per Kid, you should change your model to a single FriendList object.

For the current model, you can also do:

k.friend_list.first.members
# => ["5374a1f320db90054c0000ea", "537c63ea20db9040d2000332"]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Uri, this solve my problem, Stackoverflow say that I have to wait 12 minutes to accept this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.