在軌道控制檯上,我試圖在我的課程類中保存2個類別。但是,我的輸出返回1個類別。如何在mongodb集合中保存2個文檔而不是1個?
我的代碼:
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
class Category
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :books
end
class Author
include Mongoid::Document
field :name, type: String
has_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: 5833129213197da7660c0adb, 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> Author.create(name: "Charles Dickens")
=> #<Author _id: 5833129a13197da7660c0adc, name: "Charles Dickens">
irb(main):003:0> Category.create(name: "Fiction")
=> #<Category _id: 583312a113197da7660c0add, name: "Fiction", book_ids: nil>
irb(main):004:0> Category.create(name: "Drama")
=> #<Category _id: 583312a813197da7660c0ade, name: "Drama", book_ids: nil>
irb(main):006:0*
irb(main):007:0* b.author = Author.where(name: "Charles Dickens").first
=> #<Author _id: 5832d97713197da0893c05b9, name: "Charles Dickens">
irb(main):008:0> b.categories << Category.first
=> [#<Category _id: 58330d7d13197da0893c05ba, name: "Fiction", book_ids: [BSON::ObjectId('5832d96913197da0893c05b8'), BSON::ObjectId('5833129213197da7660c0adb')]>]
irb(main):009:0> b.categories << Category.last
=> [#<Category _id: 58330d7d13197da0893c05ba, name: "Fiction", book_ids: [BSON::ObjectId('5832d96913197da0893c05b8'), BSON::ObjectId('5833129213197da7660c0adb')]>]
irb(main):010:0>
irb(main):011:0*
irb(main):012:0* b
=> #<Book _id: 5833129213197da7660c0adb, title: "Oliver Twist", publisher: "Dover Publications", published_on: 2002-12-30 00:00:00 UTC, votes: nil, author_id: BSON::ObjectId('5832d97713197da0893c05b9'), category_ids: [BSON::ObjectId('58330d7d13197da0893c05ba')]>
irb(main):013:0> b.save
=> true
irb(main):014:0> Category.first
=> #<Category _id: 58330d7d13197da0893c05ba, name: "Fiction", book_ids: [BSON::ObjectId('5832d96913197da0893c05b8')]>
irb(main):015:0> Category.last
=> #<Category _id: 58330d7d13197da0893c05ba, name: "Fiction", book_ids: [BSON::ObjectId('5832d96913197da0893c05b8')]>
irb(main):016:0>
兩類輸出顯示的名字爲 '小說' 而不是 '戲劇' futhermore,在測試 -
> db.books.findOne()
{
"_id" : ObjectId("5833129213197da7660c0adb"),
"title" : "Oliver Twist",
"publisher" : "Dover Publications",
"published_on" : ISODate("2002-12-30T00:00:00Z"),
"author_id" : ObjectId("5832d97713197da0893c05b9"),
"category_ids" : [
ObjectId("58330d7d13197da0893c05ba")
]
}
>
>
> db.categories.findOne()
{
"_id" : ObjectId("58330d7d13197da0893c05ba"),
"name" : "Fiction",
"book_ids" : [
ObjectId("5832d96913197da0893c05b8")
]
}
如何我確保2個categorie_ids出現在db.books.findOne()上?
會感謝你的建議..
我發現加入第二類,以藏書的另一種方式。在rails控制檯上使用b.categories.create(名稱:「drama」)。 – Sharon