0

我正在关注Ruby and MongoDB Web Development一书,并尝试尽可能多地遵循这些示例,但由于某些原因,我无法使其正常工作。

这是我到目前为止所拥有的模型:

应用程序/模型/book.rb

class Book
     include Mongoid::Document

     field :title, type: String
     field :publisher, type: String
     field :published_on,  type: Date

     field :votes, type: Array

     belongs_to :author
     has_and_belongs_to_many :categories

     embeds_many :reviews
end

应用程序/模型/作者.rb

class Author
     include Mongoid::Document

     field :name, type: String

     has_many :books
end

应用程序/模型/category.rb

class Category
     include Mongoid::Document

     field :comment, type: String
     field :username, type: String

     has_and_belongs_to_many :books
end

到目前为止一切顺利,然后在 Rails 控制台中

irb(main):001:0> b  = Book.new(title: "Oliver Twist", publisher: "Dover Publications", :published_on => Date.parse("2002-12-30"))
=> #<Book _id: 53c9215c456d655abb000000, title: "Oliver Twist", publisher: "Dover Publications", published_on: 2002-12-30 00:00:00 UTC, votes: nil, author_id: nil, category_ids: nil>

irb(main):002:0> Category.create(name: 'Fiction')
=> #<Category _id: 53c92161456d655abb010000, name: "Fiction", book_ids: nil>

irb(main):004:0* Category.create(name: 'Drama')
=> #<Category _id: 53c92166456d655abb020000, name: "Drama", book_ids: nil>
irb(main):005:0> b.categories << Category.first
=> [#<Category _id: 53c92161456d655abb010000, name: "Fiction", book_ids: [BSON::ObjectId('53c9215c456d655abb000000')]>]

irb(main):006:0> b.categories << Category.last
=> [#<Category _id: 53c92161456d655abb010000, name: "Fiction", book_ids: [BSON::ObjectId('53c9215c456d655abb000000')]>, #<Category _id: 53c92166456d655abb020000, name: "Drama", book_ids: [BSON::ObjectId('53c9215c456d655abb000000')]>]

irb(main):007:0> b.save
=> true
irb(main):008:0> b
=> #<Book _id: 53c9215c456d655abb000000, title: "Oliver Twist", publisher: "Dover Publications", published_on: 2002-12-30 00:00:00 UTC, votes: nil, author_id: nil, category_ids: [BSON::ObjectId('53c92161456d655abb010000'), BSON::ObjectId('53c92166456d655abb020000')]>

irb(main):009:0> Category.first
=> #<Category _id: 53c92161456d655abb010000, name: "Fiction", book_ids: nil>
irb(main):010:0>

类别对象没有更新,发生了什么?我做错了什么?帮助!

信息: Rails 4Mongoid 4, R​​uby 2.1MongoDB 2.6

编辑1:

embedded_in :book从文件app/models/category.rb 中删除了不必要的行。

4

2 回答 2

3

您应该使用自动保存来支持以下语法:

b.categories << Category.first
b.categories << Category.last

为此,请将其添加到您的模型中

has_and_belongs_to_many :books, autosave: true

has_and_belongs_to_many :categories, autosave: true

要了解会发生什么,请尝试在控制台中使用以下代码而不进行自动保存:

b  = Book.new(title: "Oliver Twist", publisher: "Dover Publications", :published_on => Date.parse("2002-12-30"))
c1 = Category.create(name: 'Fiction')
c2 = Category.create(name: 'Drama')
b.categories << c1
b.categories << c2
b.save

c1.changed?
c2.changed?

它更改父对象,但不会自动保存。

于 2014-07-22T10:51:20.483 回答
0

我终于能够让它工作,虽然,我这样做的方式,对我来说感觉不太对劲。

代替

b.categories << Category.first
b.categories << Category.last

我用了

b.category_ids << Category.first._id
b.category_ids << Category.last._id

保存文档后,Category 对象将按预期更新。

我必须说,这感觉不太正确,因为很多人使用另一种形式,我或 Mongoid gem 一定有问题。

欢迎任何进一步的解释,并可能被接受为正确答案。

于 2014-07-21T15:48:48.683 回答