【发布时间】:2019-11-22 01:19:38
【问题描述】:
我正在构建一个简单的脚本来填充 MongoDB 数据库。这是我第一次使用 NoSQL DB,我觉得我可能是从 SQL DB 的角度考虑这个问题。
此脚本的基础是填充一个数据库,该数据库包含一些相互关联的集合。但是当我运行我的脚本时,我在构建/保存文档时看到了一个无效的错误。
我有三个系列; Book、Author 和 Style,具有以下关系。
- 一个
Book有很多Authors - 一个
Author有很多Books - 一个
Author有很多Styles - 一个
Style有很多Authors
模型定义如下:
# Book Model
class Book
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
validates :title, presence: true
has_and_belongs_to_many :authors
index({ title: 'text' })
end
# Author Model
class Author
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
validates :name, presence: true
has_and_belongs_to_many :books
has_and_belongs_to_many :styles
index({ name: 1 }, { unique: true })
end
# Style Model
class Style
include Mongoid::Document
include Mongoid::Timestamps
field :type, type: String
validates :type, presence: true
has_and_belongs_to_many :authors
index({ type: 1 }, { unique: true, name: "type_index" })
end
然后这是我的脚本:
# script.rb
book = Book.new
book.title = "Good Omens"
['Neil Gaiman', 'Terry Pratchett'].each do |author_name|
author = Author.find_by(name: author_name)
if author.nil?
author = Author.new(name: author_name)
end
# a list of writing styles this author can have
# pretend that there's a list of styles per author
literary_styles.each do |style_name|
style = Style.find_by(type: style_name)
if style.nil?
author.styles.build(Style.new(type: style_name))
else
unless author.styles.include? style.id
author.styles << style
end
end
end
author.valid? #=> false
author.errors #=> @messages={:styles=>["is invalid"]}
book.author.build(book.attributes)
book.save
end
Book 文档已创建,但由于无效的样式验证错误,Author 和 Style 不会持续存在。我希望我能确切地看到导致验证失败的原因,但消息传递非常模糊。我怀疑它来自 Author 和 Style 之间的 has_and_belongs_to_many 关系的一些内置验证,但我无法确定。
我觉得有趣的是Book 文档有一个author_ids 属性,其中填充了id,但是当我跳入控制台时,没有可以拉起或绑定到Book 的作者。
如果需要,很乐意提供更多信息。
【问题讨论】:
标签: ruby-on-rails ruby mongodb mongoid