Mongoid很多很多关系不起作用,我的模特有什么问题?

时间:2014-07-18 14:04:24

标签: ruby-on-rails ruby mongodb mongoid4

我跟随 Ruby和MongoDB Web开发这本书,并尝试尽可能地遵循这些示例,但由于某些原因我无法得到它工作

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

的应用程序/模型/ 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

的应用程序/模型/ author.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 4 Mongoid 4 Ruby 2.1 MongoDB 2.6

修改1:

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

2 个答案:

答案 0 :(得分:2)

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

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?

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

答案 1 :(得分:0)

我终于能够让它发挥作用了,尽管我这样做的方式并不适合我。

而不是

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

我用过

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

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

我必须说,这感觉不太正确,因为很多人使用其他形式,我或者Mongoid宝石都有问题。

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