Reputation: 10744
class Question
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
has_one :response
end
class Response
include Mongoid::Document
include Mongoid::Timestamps
field :content, :type => String
belongs_to :question
end
console:
1.9.3p448 :014 > Question.where(:response => nil).size
=> 3
1.9.3p448 :016 > Question.where(:response.ne => nil).size
=> 0
However all questions
has one response
created and associated!
Thanks!
Upvotes: 1
Views: 1085
Reputation: 2656
The ID field is stored in the child object only, therefore Question
has no knowledge of its Response
(no response_id
field).
You can achieve the goal like this:
Response.ne(question_id: nil).map(&:question)
Or, to fetch questions without responses:
Question.not.in(id: Response.pluck(:question_id))
Upvotes: 4