如何在mongodb集合中保存2个文件而不是1个?

时间:2016-11-21 16:14:30

标签: ruby-on-rails mongodb

在rails控制台上,我试图在我的book类中保存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> 

两个类别都将名称显示为“小说”和“小说”。不是戏剧&#39; 此外,正在测试中 -

> 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")
  ]
}

如何确保db.books.findOne()上出现2个categorie_ids?

非常感谢您的建议..

1 个答案:

答案 0 :(得分:0)

根据此document,您必须明确提供排序顺序,以使第一个和最后一个方法正常工作。

像这样,

generic parameter 'T' could not be inferred

来源:The bug of mongoid returning first document when invoking last?