Rails 和 mongo 嵌入关系
Rails and mongo embed relations
我正在尝试在 Mongoid 上创建一些关系,但是当我尝试保存内部对象或将其添加到 user.personal_accounts
集合时,出现以下错误
NoMethodError: undefined method `bson_type' for #<Bank:0x71c01a8>
我在 rails 控制台中的对象是正确的
#<PersonalAccount _id: 56e87f669c27691be0d3041b, number: "55", active: true, bank: #<Bank _id: 56d74cdb9c27692fb4bd4c6d, code: 123, name: "Bradesco", country: "USA">>
我的映射
class PersonalAccount
include Mongoid::Document
field :number, type: String
field :active, type: Boolean
field :bank, type: Bank
embedded_in :user
end
class User
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
embeds_many :personal_accounts
end
class Bank
include Mongoid::Document
field :code, type: Integer
field :name, type: String
field :country, type: String
end
我期待的映射是:
- 用户
- 个人账户
- 银行
- 银行
正如我所读到的,我需要将外部银行复制到每个 PersonalAccount。
我已经尝试过以下Link
安装的版本:
bson (4.0.2)
bson_ext (1.5.1)
mongoid (5.0.2)
mongo (2.2.4)
问题的根源就在这里:
field :bank, type: Bank
MongoDB 不知道如何存储 Bank
所以 Mongoid 会尝试将它转换成 MongoDB 可以理解的东西,同时 Mongoid 正在为数据库准备数据,因此 NoMethodError
.
您可能希望 Bank
作为自己的集合存在,然后每个 PersonalAccount
将引用一个 Bank
。那将是一个标准的 belongs_to
设置:
class PersonalAccount
#... but no `field :bank`
belongs_to :bank
end
这将在幕后添加一个 field :bank_id, :type => BSON::ObjectId
到 PersonalAccount
,并为您连接访问器 (bank
) 和修改器 (bank=
) 方法。
通常你会想要 Bank
中关系的另一半:
class Bank
#...
has_many :personal_accounts
end
但这行不通(如您所见),因为 PersonalAccount
嵌入在 User
中,所以 Bank
无法直接获取它。请记住,embeds_one
只是一种将 Mongoid 机器包装在文档中的 Hash
字段周围的奇特方式,而 embeds_many
只是一种将 Mongoid 机器包装在哈希数组周围的奇特方式在另一个文档中;嵌入式文档没有独立存在,它们只是其父文档的一部分。
我正在尝试在 Mongoid 上创建一些关系,但是当我尝试保存内部对象或将其添加到 user.personal_accounts
集合时,出现以下错误
NoMethodError: undefined method `bson_type' for #<Bank:0x71c01a8>
我在 rails 控制台中的对象是正确的
#<PersonalAccount _id: 56e87f669c27691be0d3041b, number: "55", active: true, bank: #<Bank _id: 56d74cdb9c27692fb4bd4c6d, code: 123, name: "Bradesco", country: "USA">>
我的映射
class PersonalAccount
include Mongoid::Document
field :number, type: String
field :active, type: Boolean
field :bank, type: Bank
embedded_in :user
end
class User
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
embeds_many :personal_accounts
end
class Bank
include Mongoid::Document
field :code, type: Integer
field :name, type: String
field :country, type: String
end
我期待的映射是:
- 用户
- 个人账户
- 银行
- 个人账户
- 银行
正如我所读到的,我需要将外部银行复制到每个 PersonalAccount。
我已经尝试过以下Link
安装的版本:
bson (4.0.2)
bson_ext (1.5.1)
mongoid (5.0.2)
mongo (2.2.4)
问题的根源就在这里:
field :bank, type: Bank
MongoDB 不知道如何存储 Bank
所以 Mongoid 会尝试将它转换成 MongoDB 可以理解的东西,同时 Mongoid 正在为数据库准备数据,因此 NoMethodError
.
您可能希望 Bank
作为自己的集合存在,然后每个 PersonalAccount
将引用一个 Bank
。那将是一个标准的 belongs_to
设置:
class PersonalAccount
#... but no `field :bank`
belongs_to :bank
end
这将在幕后添加一个 field :bank_id, :type => BSON::ObjectId
到 PersonalAccount
,并为您连接访问器 (bank
) 和修改器 (bank=
) 方法。
通常你会想要 Bank
中关系的另一半:
class Bank
#...
has_many :personal_accounts
end
但这行不通(如您所见),因为 PersonalAccount
嵌入在 User
中,所以 Bank
无法直接获取它。请记住,embeds_one
只是一种将 Mongoid 机器包装在文档中的 Hash
字段周围的奇特方式,而 embeds_many
只是一种将 Mongoid 机器包装在哈希数组周围的奇特方式在另一个文档中;嵌入式文档没有独立存在,它们只是其父文档的一部分。