Rails 和 Mongoid 的动态属性

发布于 2024-08-30 00:35:51 字数 717 浏览 2 评论 0原文

我正在通过 Mongoid Ruby gem 和 Rails (Rails 3 beta 3) 学习 MongoDB,并且我试图想出一种方法,在基于另一个模型的字段的模型上创建动态属性,我认为这是一个模式 -较少的数据库将是一个不错的选择。

例如,我有模型:

class Account
  include Mongoid::Document

  field :name, :type => String
  field :token, :type => String
  field :info_needed, :type => Array

  embeds_many :members
end

class Member
  include Mongoid::Document

  embedded_in :account, :inverse_of => :members

end

我希望采用帐户模型的“info_needed”属性,并根据内部内容在会员模型上创建动态属性。如果club.info_needed是[“first_name”,“last_name”],我试图创建一个将first_name和last_name属性保存到Member模型的表单。

然而,在实践中,当我尝试执行此操作时,我只是在成员模型上不断收到“未定义的方法first_name =”错误。我知道 MongoDB 可以处理每条记录的动态属性,但是如何让 Mongoid 做到这一点而不出现未定义的方法错误?

I'm learning MongoDB through the Mongoid Ruby gem with Rails (Rails 3 beta 3), and I'm trying to come up with a way to create dynamic attributes on a model based on fields from another model, which I thought a schema-less database would be a good choice for.

So for example, I'd have the models:

class Account
  include Mongoid::Document

  field :name, :type => String
  field :token, :type => String
  field :info_needed, :type => Array

  embeds_many :members
end

class Member
  include Mongoid::Document

  embedded_in :account, :inverse_of => :members

end

I'm looking to take the "info_needed" attribute of the Account model and created dynamic attributes on the Member model based on what's inside. If club.info_needed was ["first_name", "last_name"], I'm trying to create a form that would save first_name and last_name attributes to the Member model.

However, upon practice, I just keep getting "undefined method first_name=" errors on the Member model when trying to do this. I know MongoDB can handle dynamic attributes per record, but how can I get Mongoid to do this without an undefined method error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

涫野音 2024-09-06 00:35:51

Mongoid 现在支持动态字段。他们的文档可以在这里找到:
http://mongoid.org/en/mongoid/docs/documents.html#dynamic_fields基本上

,它警告您必须稍微小心如何设置动态字段,因为如果您尝试对文档中不存在的字段使用 getter 和 setter 方法,则会引发无方法错误。

[],[]= 是 read_attribute(),write_attribute() 的快捷方式,如果您未在 ./config/mongoid.yml 文件中设置 dynamic_attributes = true ,则应使用它们< /code> ,否则你会得到一个 no method 错误。

设置 allow_dynamic_fields: true 可能存在风险,因为代码中的错误可能会导致意外字段污染您的数据/架构。将其设置为 false 并显式使用 [],[]= 可能更安全

# Raise a NoMethodError if value isn't set.
person.gender
person.gender = "Male"

# Retrieve a dynamic field safely.
person[:gender]
person.read_attribute(:gender)

# Write a dynamic field safely.
person[:gender] = "Male"
person.write_attribute(:gender, "Male")

Mongoid now supports Dynamic Fields. Their documentation can be found here:
http://mongoid.org/en/mongoid/docs/documents.html#dynamic_fields

Basically it warns that you have to be slightly careful how you set dynamic fields as it will raise a no method error if you attempt to use the getter and setter methods for a field that did not exist in the document.

[],[]= are shortcuts for read_attribute(),write_attribute() , and should be used if you do not set dynamic_attributes = true in your ./config/mongoid.yml file , otherwise you'll get a no method error.

Setting allow_dynamic_fields: true can be risky, as you might pollute your data/schema with unintended fields caused by bugs in your code. It's probably safer to set this to false and explicitly use [],[]=

# Raise a NoMethodError if value isn't set.
person.gender
person.gender = "Male"

# Retrieve a dynamic field safely.
person[:gender]
person.read_attribute(:gender)

# Write a dynamic field safely.
person[:gender] = "Male"
person.write_attribute(:gender, "Male")
乄_柒ぐ汐 2024-09-06 00:35:51

请务必在 mongoid.yml 中设置 allow_dynamic_fields: true。例子:

defaults: &defaults
  allow_dynamic_fields: true
  parameterize_keys: true
  persist_in_safe_mode: true
  raise_not_found_error: true
  reconnect_time: 3
  use_object_ids: false

development:
  <<: *defaults

...

Be sure to set allow_dynamic_fields: true in mongoid.yml. Example:

defaults: &defaults
  allow_dynamic_fields: true
  parameterize_keys: true
  persist_in_safe_mode: true
  raise_not_found_error: true
  reconnect_time: 3
  use_object_ids: false

development:
  <<: *defaults

...
眼眸印温柔 2024-09-06 00:35:51

关于 Mongoid 和 Rails 3.1 的动态属性的有趣文章: http://paul-wong-jr.blogspot.com/2012/03/dynamic-attributes-and-mongodbmongoid.html

要仅访问动态键/值对或动态属性名称,另请参阅:
列出 Mongoid 模型中的动态属性

interesting article about Dynamic Attributes with Mongoid and Rails 3.1: http://paul-wong-jr.blogspot.com/2012/03/dynamic-attributes-and-mongodbmongoid.html

To access only the dynamic key/value pairs or dynamic attribute names, see also:
List dynamic attributes in a Mongoid Model

梦中的蝴蝶 2024-09-06 00:35:51

Mongoid 并不真正支持它。

我碰巧有人询问< /a> 我自己在 Mongoid 小组。

当您创建新文档时,可能会出现这样的情况:

account = Account.new(:some_dynamic_field => "...")

Mongoid doesn't really support it.

I happen to have asked this at Mongoid group myself.

It is possible when you create new document, like this:

account = Account.new(:some_dynamic_field => "...")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文