嵌套属性未保存为 Mongoid 中的references_one

发布于 2024-10-07 19:31:49 字数 3607 浏览 3 评论 0原文

我昨天工作正常,做了一些更改,但我不知道我是如何破坏它的。我确信某处有错字,但我看不到它。

对表单中的嵌套模型所做的任何更改根本不会被保存,开发日志显示正在经历的属性 - 并且它们似乎采用正确的格式,但它根本没有更新。

我有一个 User 模型,它引用_one Biography 如下:

# app/models/user.rb
class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :first_name, :type => String
  field :last_name, :type => String
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

  references_one :biography

  accepts_nested_attributes_for :biography
  #--snip---
end

# app/models/biography.rb
class Biography
  include Mongoid::Document
  include Mongoid::Timestamps

  field :content, :type => String
  field :role, :type => String
  field :is_crew, :type => Boolean

  referenced_in :user
end

最后是我的表单(我在这里使用简单的表单,但在大多数情况下,它的行为与 formattastic 非常相似) :

<%= simple_form_for [:manage, @user], :html => {:multipart => true}, :position => :left do |f| %>
  <h2>Login details</h2>
  <%= f.input :email, :input_html => {:class => "text"} %>      
  <%= f.input :first_name, :input_html => {:class => "text"} %>
  <%= f.input :last_name, :input_html => {:class => "text"} %>

  <div class="biography">
    <h2>Biography</h2>
    <%= f.simple_fields_for :biography do |biography_form| %>
      <%= biography_form.input :role, :input_html => {:class => "text"} %>
      <%= biography_form.input :content, :as => :text, :input_html => {:class => "textarea"} %>
      <%= biography_form.input :is_crew, :as => :boolean %>
    <%- end -%>
  </div>

  <%= f.submit "Save user", :class => "submit mid" %>
<% end %>

以及我的开发日志的一些输出,以防答案就在那里,而我只是看不到它们:

    Started POST "/manage/users/john-doe" for 127.0.0.1 at Wed Dec 15 11:42:09 +1100 2010
      Processing by Manage::UsersController#update as HTML
      Parameters: {"commit"=>"Save user", "authenticity_token"=>"44QlHsbKb8Pm91wnxWJa8Y0QsUXDzp/3rVpfs3G1Inc=", "utf8"=>"✓", "id"=>"john-doe", "user"=>{"biography_attributes"=>{"is_crew"=>"0", "role"=>"Tenor", "id"=>"4d080de56a4f1dfe7700000e", "content"=>"John was born on the 1st of January, 1970."}, "last_name"=>"Doe", "first_name"=>"Johnathan", "email"=>"[email protected]"}}
    the_idea_of_north_development['users'].find({:_id=>BSON::ObjectId('4d06e6036a4f1dcb1b000001')}, {}).limit(-1)
    the_idea_of_north_development['users'].find({:slug=>"john-doe"}, {}).limit(-1)
    the_idea_of_north_development['biographies'].find({"user_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {}).limit(-1)
    the_idea_of_north_development['$cmd'].find({"count"=>"users", "query"=>{:_id=>{"$ne"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, :email=>/^testing@involved\.com\.au$/i}, "fields"=>nil}, {}).limit(-1)
    the_idea_of_north_development['users'].find({"slug"=>"johnathan-doe"}, {})
    MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})
    Redirected to http://lvh.me:3000/manage/users
    Completed 302 Found in 17ms

User 模型的任何更改都可以更新,但对 Biography 的更改未保存。帮助我的心灵,你是我唯一的希望!

I had this working fine yesterday, made some changes and I have no idea how I broke it. I'm sure it's a typo somewhere, but I cannot see it.

Any changes made to the nested model in the form are just not being saved at all, the development log shows the attributes going through - and they appear to be in the right format, but it's just not updating at all.

I have a User model which references_one Biography like so:

# app/models/user.rb
class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :first_name, :type => String
  field :last_name, :type => String
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

  references_one :biography

  accepts_nested_attributes_for :biography
  #--snip---
end

# app/models/biography.rb
class Biography
  include Mongoid::Document
  include Mongoid::Timestamps

  field :content, :type => String
  field :role, :type => String
  field :is_crew, :type => Boolean

  referenced_in :user
end

And finally my form (i'm using simple form here, but for the most part it behaves very similar to formtastic):

<%= simple_form_for [:manage, @user], :html => {:multipart => true}, :position => :left do |f| %>
  <h2>Login details</h2>
  <%= f.input :email, :input_html => {:class => "text"} %>      
  <%= f.input :first_name, :input_html => {:class => "text"} %>
  <%= f.input :last_name, :input_html => {:class => "text"} %>

  <div class="biography">
    <h2>Biography</h2>
    <%= f.simple_fields_for :biography do |biography_form| %>
      <%= biography_form.input :role, :input_html => {:class => "text"} %>
      <%= biography_form.input :content, :as => :text, :input_html => {:class => "textarea"} %>
      <%= biography_form.input :is_crew, :as => :boolean %>
    <%- end -%>
  </div>

  <%= f.submit "Save user", :class => "submit mid" %>
<% end %>

And some output from my development log, incase the answers lie there, and I just cant see them:

    Started POST "/manage/users/john-doe" for 127.0.0.1 at Wed Dec 15 11:42:09 +1100 2010
      Processing by Manage::UsersController#update as HTML
      Parameters: {"commit"=>"Save user", "authenticity_token"=>"44QlHsbKb8Pm91wnxWJa8Y0QsUXDzp/3rVpfs3G1Inc=", "utf8"=>"✓", "id"=>"john-doe", "user"=>{"biography_attributes"=>{"is_crew"=>"0", "role"=>"Tenor", "id"=>"4d080de56a4f1dfe7700000e", "content"=>"John was born on the 1st of January, 1970."}, "last_name"=>"Doe", "first_name"=>"Johnathan", "email"=>"[email protected]"}}
    the_idea_of_north_development['users'].find({:_id=>BSON::ObjectId('4d06e6036a4f1dcb1b000001')}, {}).limit(-1)
    the_idea_of_north_development['users'].find({:slug=>"john-doe"}, {}).limit(-1)
    the_idea_of_north_development['biographies'].find({"user_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {}).limit(-1)
    the_idea_of_north_development['$cmd'].find({"count"=>"users", "query"=>{:_id=>{"$ne"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, :email=>/^testing@involved\.com\.au$/i}, "fields"=>nil}, {}).limit(-1)
    the_idea_of_north_development['users'].find({"slug"=>"johnathan-doe"}, {})
    MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})
    Redirected to http://lvh.me:3000/manage/users
    Completed 302 Found in 17ms

Any changes to the User model update fine, but changes to the Biography are unsaved. Help me hive-mind, you're my only hope!

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

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

发布评论

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

评论(2

亣腦蒛氧 2024-10-14 19:31:49

我也遇到过类似的问题,即嵌套文档的更改未被保留。 (问题是 如何更新嵌套 Mongo 文档属性在 Rails 中使用 Mongoid?)。我对 Mongoid 本身表示怀疑,因为更新嵌入文档时永远不会记录如下消息:

MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})

如果尝试更新嵌套文档,则会出现类似的行选择父文档并设置嵌套文档的值,例如: 就我

the_idea_of_north_development['users'].update({"_id"=>PARENT_DOCUMENT}, {"$set"=>{"biography_attributes.is_crew"=>"0", "biography_attribute.role"=>"Tenor", etc...})

个人而言,我发现 Mongoid 问题 #357 可能是原因。

I've been having a similar problem with changes to nested documents not being persisted. (The question is How Do I Update Nested Mongo Document Attributes in Rails with Mongoid?). I'm suspicious of Mongoid itself because a message like the following is never logged for updating the embedded document:

MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})

If an attempt was being made to make an update to the nested document, there would be a line similar to that one selecting the parent document and setting the nested document's values, such as:

the_idea_of_north_development['users'].update({"_id"=>PARENT_DOCUMENT}, {"$set"=>{"biography_attributes.is_crew"=>"0", "biography_attribute.role"=>"Tenor", etc...})

Personally, I find Mongoid Issue #357 a possible reason.

醉酒的小男人 2024-10-14 19:31:49

在同一个问题上被杀了两周。
我设法做的唯一修复如下。
在用户控制器中,执行以下操作:

if @user.save
params[:user][:biography_attributes].each do |bio|
         @biography = @user.biography.build({ :role => bio[:role], :iscrew => bio[:iscrew] })
    end

您将迭代传记的所有实例并将其显式保存到数据库中。

Killed two weeks on same issue.
The only fix I've managed to do is as follows.
In User controller, do this:

if @user.save
params[:user][:biography_attributes].each do |bio|
         @biography = @user.biography.build({ :role => bio[:role], :iscrew => bio[:iscrew] })
    end

You'll iterate over all instances of biography and eplicitly save it into DB.

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