user934801
user934801

Reputation: 1139

Mongoid embedded query

Hi I have embedded documents but i can't query them by id. I can output all with inspect and there are the right ids but querying them is not possible. I have a Course Model which embeds_many :course_members and a CourseMember Model with embedded_in :course, :inverse_of => :course_members

I tried this without any success:

puts Course.where("course_members.id" => params[:id])

With this code i can reach the rigth document:

c = Course.where("course_members.accepted" => 2).all
c.each do |l|
 l.course_members.each do |f|
  puts f.inspect
 end
end

But how can i get my data with one Mongoid query?

Upvotes: 1

Views: 1315

Answers (1)

Thomas Guillory
Thomas Guillory

Reputation: 5729

I guess

puts Course.where("course_members._id" => BSON::ObjectId(params[:id]))

The real id argument in MongoDB is _id, the function .id in mongoid is just a convenient wrapper.

Edit: You also have to convert your string params[:id] to a correct BSON ID.

Upvotes: 6

Related Questions