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

アプリ/モデル/author.rb

class Author
     include Mongoid::Document

     field :name, type: String

     has_many :books
end

アプリ/モデル/カテゴリ.rb

class Category
     include Mongoid::Document

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

     has_and_belongs_to_many :books
end

これまでのところ、レールコンソールで

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 4Ruby 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 に答える