Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it a bug of Mongoid or Rails3 ? Here are full codes that you can reproduce the strange 'bug'
    primarykey
    data
    text
    <pre><code>$ rails -v Rails 3.1.1 $ ruby -v ruby 1.9.2p312 (2011-08-11 revision 32926) [i686-linux] </code></pre> <p><strong>If you want to reproduce the problem, just follow me:</strong> </p> <p><strong>First, create these three model(just copy):</strong> </p> <pre><code>#school.rb class School include Mongoid::Document include Mongoid::Timestamps has_many :students end </code></pre> <hr> <pre><code>#student.rb class Student include Mongoid::Document include Mongoid::Timestamps has_many :books belongs_to :school accepts_nested_attributes_for :books end </code></pre> <hr> <pre><code>#book.rb class Book include Mongoid::Document include Mongoid::Timestamps field :name belongs_to :student validate :check def check # The calling for the 'school' method caused the issue self.student.school end end </code></pre> <p><strong>Second, run your console and paste:</strong> </p> <pre><code>ruby-1.9.2-head :001 &gt; School.destroy_all;Student.destroy_all; Book.destroy_all; School.create ruby-1.9.2-head :001 &gt; Student.create school_id: School.first.id, 'books_attributes' =&gt; {'1' =&gt; {'name' =&gt; 'I am a book'}} </code></pre> <hr> <p>Then, let's see what happend: </p> <pre><code>ruby-1.9.2-head :002 &gt; Book.count MONGODB xxx_development['$cmd'].find({"count"=&gt;"books", "query"=&gt;{}, "fields"=&gt;nil}) =&gt; 2 </code></pre> <p>And even more, if you set the 'student has_many books' relation to 'autosave: true':</p> <pre><code>class Student ...... has_many :books, autosave: true ...... end </code></pre> <p><strong>Let's see what will happend:</strong> </p> <pre><code>ruby-1.9.2-head :001 &gt; School.destroy_all;Student.destroy_all; Book.destroy_all; School.create ruby-1.9.2-head :001 &gt; Student.create school_id: School.first.id, 'books_attributes' =&gt; {'1' =&gt; {'name' =&gt; 'I am a book'}} ruby-1.9.2-head :002 &gt; Student.count MONGODB xxx_development['$cmd'].find({"count"=&gt;"students", "query"=&gt;{}, "fields"=&gt;nil}) =&gt; 2 ruby-1.9.2-head :004 &gt; Student.all.to_a MONGODB xxx_development['students'].find({}) =&gt; [#&lt;Student _id: 4f62a8341d41c81bc6000002, _type: nil, created_at: 2012-03-16 02:40:52 UTC, updated_at: 2012-03-16 02:40:52 UTC, school_id: BSON::ObjectId('4f62a8341d41c81bc6000001')&gt;, #&lt;Student _id: 4f62a8341d41c81bc6000003, _type: nil, created_at: 2012-03-16 02:40:52 UTC, updated_at: 2012-03-16 02:40:52 UTC, school_id: nil&gt;] ruby-1.9.2-head :005 &gt; Book.count MONGODB xxx_development['$cmd'].find({"count"=&gt;"books", "query"=&gt;{}, "fields"=&gt;nil}) =&gt; 2 ruby-1.9.2-head :006 &gt; Book.all.to_a MONGODB xxx_development['books'].find({}) =&gt; [#&lt;Book _id: 4f62a8341d41c81bc6000003, _type: nil, created_at: 2012-03-16 02:40:52 UTC, updated_at: 2012-03-16 02:40:52 UTC, name: "I am a book", student_id: BSON::ObjectId('4f62a8341d41c81bc6000002')&gt;, #&lt;Book _id: 4f62a8341d41c81bc6000002, _type: nil, created_at: 2012-03-16 02:40:52 UTC, updated_at: 2012-03-16 02:40:52 UTC, name: nil, student_id: nil&gt;] </code></pre> <p>This bug really run me crazy. Why there are <strong>additional models</strong> when calling 'school' in a book validate method?</p> <p>Or there is something I did wrong?</p>
    singulars
    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.
    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