views:

31

answers:

2

I have a Three documents, here is a sample with fields not shown

class College
  include Mongoid::Document
  references_many :students,:stored_as => :array, :inverse_of => :colleges
end

class Student
  include Mongoid::Document

  embedded_in :college, :inverse_of => :students
  embeds_one :mark
end

class Mark
  include Mongoid::Document

  embedded_in :student, :inverse_of => :mark
end

Now when I perform the search like this in console

@college = College.find('4cb2a6457adf3500dd000089').students.where('mark.total' => '100').first.name

gives me nil as there is no any students with total marks == 100

provided that college exists but the same code raises error in my actual code as

ERROR NoMethodError: undefined method `where' for Array:0x00000107441a30

Any ideas why this is happening? OR have i done some thing wrong?

Thanks

+1  A: 
references_many :students,:stored_as => :array

means that the value returned by the students call here

College.find('4cb2a6457adf3500dd000089').students

is an array, not a chainable Criteria. You need to split the query into two statements.

Simone Carletti
thanks for the response, but i still need to query on the students array though. Please give me some idea how to query on the array of students obj so that i can find the students with marks = 100. And it would be helpful if i know what stored_at => :array does? Is it the performance issue or some thing else.
Gagan
I kinda have the same problem. what exactly is the purpose of stored_as? cause using it will cause other problems
Allen Bargi