1

I just started a new project in Rails 4 and Mongoid 4 beta and an old behaviour that I used a lot in Mongoid 3 is not working anymore.

Before I used to write Model.only("field").to_a and I would get an array with id and field, all other fields were set to null.

If I try to do this in Mongoid 4 I get: (Object doesn't support #inspect)

Model.only("field").map {|e| e.field} is working although not as before. The id is not included anymore, I get ActiveModel::MissingAttributeError if I try to access the id.

I know I can use Model.pluck("field"), this will not return an array of documents though.

Are these changes real or am I missing something?

EDIT: As I'm writing this I tried including the id and it's working. ie. Model.only("id", "field").to_a working as before, but my question is still valid. Do I have to include the id now in order to get an array of documents?

Oktav
  • 2,143
  • 2
  • 20
  • 33

1 Answers1

0

This is a new behaviour on Mongoid 4. As you said you can add the "id" field to the only method, and it should work. You also can use the pluck method to get and array of fields you want. Something like:

Model.all.pluck("id", "field"). As you said, You already knew about pluck, and thats the way to go..

cheers.

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73