Ruby on rails 3 是Mongoid还是Rails3的bug?下面是完整的代码,您可以复制奇怪的';bug';

Ruby on rails 3 是Mongoid还是Rails3的bug?下面是完整的代码,您可以复制奇怪的';bug';,ruby-on-rails-3,mongodb,mongoid,nested-attributes,autosave,Ruby On Rails 3,Mongodb,Mongoid,Nested Attributes,Autosave,如果您想重现问题,请跟我来: #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 en

如果您想重现问题,请跟我来:

#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
首先,创建这三个模型(只需复制):

$ rails -v
Rails 3.1.1
$ ruby -v
ruby 1.9.2p312 (2011-08-11 revision 32926) [i686-linux]


其次,运行控制台并粘贴:

#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

然后,让我们看看发生了什么:

ruby-1.9.2-head :001 > School.destroy_all;Student.destroy_all; Book.destroy_all; School.create
ruby-1.9.2-head :001 >  Student.create school_id: School.first.id, 'books_attributes' => {'1' => {'name' => 'I am a book'}}
更重要的是,如果您将“学生有很多书”关系设置为“自动保存:true”:

ruby-1.9.2-head :002 > Book.count
MONGODB xxx_development['$cmd'].find({"count"=>"books", "query"=>{}, "fields"=>nil})
=> 2 
让我们看看会发生什么:

class Student
 ......
 has_many :books, autosave: true
 ......
end
ruby-1.9.2-head:001>School.all;学生,摧毁一切;毁灭一切;学校,创造
ruby-1.9.2-head:001>Student.create school\u id:school.first.id,'books\u attributes'=>{'1'=>{'name'=>'I am a book'}
ruby-1.9.2-head:002>Student.count
MONGODB xxx_开发['$cmd'].查找({“计数”=>“学生”,“查询”=>{},“字段”=>nil})
=> 2 
ruby-1.9.2-head:004>Student.all.to_a
MONGODB xxx_开发['students'].查找({})
=> [#, #] 
ruby-1.9.2-head:005>Book.count
MONGODB xxx_开发['$cmd'].查找({“count”=>“books”,“query”=>{},“fields”=>nil})
=> 2
ruby-1.9.2-head:006>Book.all.to_a
MONGODB xxx_开发['books'].查找({})
=> [#, #] 
这只虫子真让我抓狂。 在书籍验证方法中调用“学校”时,为什么会有其他模型


或者是我做错了什么?

这里的代码很好,您没有做任何不正确的事情-但是Mongoid对master或2.4.x上的相同代码没有问题。请参见我的建议,以找到罪魁祸首:


谢谢Durran,当我创建一个新的空rails应用程序来测试它时,问题仍然存在;但是在从github将我的mongoid(2.3.x)更新到最新版本后,现在一切正常了。:)
class Student
 ......
 has_many :books, autosave: true
 ......
end
ruby-1.9.2-head :001 > School.destroy_all;Student.destroy_all; Book.destroy_all; School.create
ruby-1.9.2-head :001 >  Student.create school_id: School.first.id, 'books_attributes' => {'1' => {'name' => 'I am a book'}}
ruby-1.9.2-head :002 > Student.count
MONGODB xxx_development['$cmd'].find({"count"=>"students", "query"=>{}, "fields"=>nil})
 => 2 

ruby-1.9.2-head :004 > Student.all.to_a
MONGODB xxx_development['students'].find({})
 => [#<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')>, #<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>] 

ruby-1.9.2-head :005 > Book.count
MONGODB xxx_development['$cmd'].find({"count"=>"books", "query"=>{}, "fields"=>nil})
 => 2
ruby-1.9.2-head :006 > Book.all.to_a
MONGODB xxx_development['books'].find({})
 => [#<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')>, #<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>]