Retrieving array of ids in Mongoid

Remember that the ID is stored as :_id and not :id . There is an id helper method, but when you do queries, you should use :_id:

User.where(:_id.in => arr)

Often I find it useful to get a list of ids to do complex queries, so I do something like:

user_ids = User.only(:_id).where(:foo => :bar).distinct(:_id)
Post.where(:user_id.in => user_ids)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)