Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange mongoid error "Mongoid::Errors::InvalidCollection: ... it is an embedded document, please access a collection from the root document."
    primarykey
    data
    text
    <p>I am getting this error in my rails 3 application. </p> <pre><code>Mongoid::Errors::InvalidCollection: Access to the collection for Match is not allowed since it is an embedded document, please access a collection from the root document. </code></pre> <p>Here is the corresponding code structure</p> <p>Match class</p> <pre><code>Stateflow.persistence = :mongoid class Match &lt; GmMongo::Base include Stateflow #attributes field :name, :type=&gt;String field :match_type, :type=&gt;String field :state #state transistions stateflow do state_column :state initial :unlocked state :unlocked state :locked event :lock do transitions :from =&gt; :unlocked, :to =&gt; :locked end event :unlock do transitions :from =&gt; :locked, :to =&gt; :unlocked end end #relations embeds_many :players has_many :messages, :class_name=&gt;:match_messages.to_s.classify belongs_to :game embeds_many :groups #validations validates_presence_of :name validates_presence_of :match_type def send_message(match_message) # #TODO: raise exception if the argument is invalid. i.e. Not of class MatchMessage options = {} unless match_message.target_user.blank? options = { :audience=&gt;:single_player, :target_player=&gt;match_message.target_user } else unless match_message.group.blank? options = { :audience=&gt;:group, :group=&gt;match_message.group } else options = { :audience=&gt;:match, :match=&gt;match_message.match } end end args = OpenHash[{ :message=&gt;match_message.content, :game_id=&gt;match_message.match.game.id, }.merge(options)] Message.send_notification(args) #Set the sent_at attribute for message being sent. match_message.sent_at=Time.now match_message.save! end def get_devices self.players.collect{|player| player.device} end end </code></pre> <p>Player class</p> <pre><code>class Player include Mongoid::Document #belongs_to :user #belongs_to :device field :user_id, :type=&gt;BSON::ObjectId field :device_id, :type=&gt;BSON::ObjectId has_many :sent_messages, :class_name=&gt;"MatchMessage", :foreign_key=&gt;:sender_id has_many :targeted_messages, :class_name=&gt;"MatchMessage", :foreign_key=&gt;:target_user_id embedded_in :match embedded_in :group validates_presence_of :user validates_presence_of :device validates_presence_of :match validates_uniqueness_of :user_id, :scope =&gt; [:device_id, :match_id], :message=&gt;"Player already exists." #A workaround for mixing relational and embedded references def user=(user) self[:user_id]=user.id end def user User.find(self.user_id) unless self.user_id.blank? end def device=(device) self[:device_id]=device.id end def device Device.find(self.device_id) unless self.device_id.blank? end end </code></pre> <p>Group class</p> <pre><code>class Group &lt; GmMongo::Base field :name, :type=&gt;String embedded_in :match #embeds_many :players #has_many :messages, :class_name=&gt;:match_messages.to_s.classify validates_presence_of :name, :message=&gt;"Please provide a name for this group." validates_uniqueness_of :name, :scope =&gt; [:match_id], :message=&gt;"This group in this match already exists." def get_devices self.players.collect{|player| player.device} end end </code></pre> <p>Group and Player are embedded into Match. </p> <p>When I try to run</p> <pre><code>Match.first.groups.new </code></pre> <p>I got this error</p> <pre><code>Mongoid::Errors::InvalidCollection: Access to the collection for Match is not allowed since it is an embedded document, please access a collection from the root document. </code></pre> <p>Any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload