I have a model Tag which potentially belongs to several other models, but at the moment only one model Todo which in turn belongs to User like so:
class User
include Mongoid::Document
field: name, type: String
has_many :todos
end
class Todo
include Mongoid::Document
field: name, type: String
belongs_to :user
end
class Tag
include Mongoid::Document
field: name, type: String
belongs_to :todos
end
How can I query all Tags that belongs to a particular user? I've written the following:
todo_ids = Todo.where(user_id: '86876876787')
and then:
tags = Tag.where('todo_id.in': todo_ids)
But those didn't work. What am I missing?
You're missing two things:
todo_idsin theTagquery.'todo_id.in'is a field path that is trying to look at theinfield inside atodo_idhash, this isn't a use of MongoDB's$inoperator.You can only work with one collection at a time so to fix the first one, you need to pull an array of IDs out of MongoDB:
To fix the second one, use the
$inoperator: