I have a User model with a related EnrollmentInformation model:
class User
include Mongoid::Document
has_one :enrollment_information
end
class EnrollmentInformation
include Mongoid::Document
belongs_to :user
field :type_one, :type => Boolean
end
I’d like to find all users where the EnrollmentInformation‘s type_one field’s value is true.
I’ve tried several variations on this without success. Does anyone know how I can make this query?
User.includes(:enrollment_information).where(:"enrollment_information.type_one" => true)
EDIT:
Updated query
In this situation I would consider embedding the EnrollmentInformation document inside the User, rather than setting up a relationship. Especially since it’s a has_one relationship anyway. That way you can do something like:
Here’s the documentation for embedding one document: http://mongoid.org/en/mongoid/docs/relations.html#embeds_one
As a side note, when you include, it means you are also fetching the related document. If you have Identity Map enabled, it will also be cached in there. The where clause will only operate on attributes of the User objects, and hence, if you use embedded, you will be able to access the embedded attributes.