是否有最佳做法/一致的方式来更新包含散列键值存储的数据库字段? [英] Is there a best practices/coherent way to update a database field that contains a hash key-value store?

查看:152
本文介绍了是否有最佳做法/一致的方式来更新包含散列键值存储的数据库字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是Rails 3.2的数据存储功能,其中可以选择将键值存储存储在文本字段中,即使您使用的关系数据库(如MySQL)也可以在编程时工作正常



但是有什么文件可以从RESTful HTML表单更新这些字段?或者这是不推荐的东西?也就是说,更好的解决方案是去NoSQL?

解决方案

虽然这个问题是老旧的, ,也是在ruby和rails中相当新的,所以可能会有更好的方法来做到这一点。



在模型中:

 #user.rb 

attr_accessible:preferences
store:preferences

然后以partial的形式:

 #views / users / _form .rb 

<%@ user.preferences.each do | k,v | %>
<%form.fields_for:preferences,@ user.preferences [k] do | p | %>
< div class =field>
<%= p.label k%>
< br />
<%= p.text_field k,:value => v%>
< / div>
<%end%>
<%end%>

现在,在模型中添加一些额外的字段从ive创建2 attr_accessor:

  attr_accessible ...,:new_pref_key,:new_pref_val 
attr_accessor ...,:new_pref_key,:new_pref_val

然后在表单上添加了2个新字段

 <%= f.label:new_pref_key%> 
<%= f.text_field:new_pref_key%>
<%= f.label:new_pref_val%>
<%= f.text_field:new_pref_val%>我的控制器上的

我做了一个功能,检查新字段的存在,然后合并以前的值的新的,如下所示:

 #users_controller.rb 
...
new_key = params [:user] [:preferences] [:new_pref_key]
new_val = params [:user] [:preferences] [:new_pref_val]
new_preference = {
new_key => new_val
}
current_params = params [:user] [:preferences] .merge! new_preference
...

完成,我把它传递给update_attributes,希望它帮助了!


I'm referring to Rails 3.2's Data Store feature, in which there's the option to store key-value stores in a textfield, even if you're using a relational database like MySQL...it works fine when programmatically manipulating the fields.

But what documentation is there to update these fields from a RESTful HTML form? Or is this something that's not recommended at all? That is, the better solution would be to go to NoSQL?

解决方案

although the question is quite old someone else might find it useful, also im pretty new in ruby and rails so there might be a better way to do this.

In the model:

#user.rb

attr_accessible :preferences
store :preferences

then in the form partial:

#views/users/_form.rb

<% @user.preferences.each do |k, v| %>
  <% form.fields_for :preferences, @user.preferences[k] do |p| %>
      <div class="field">
        <%= p.label k %>
        <br/>
        <%= p.text_field k, :value => v %>
      </div>
  <% end %>
<% end %>

Now to add some extra fields from the form ive created 2 attr_accessor in the model:

attr_accessible ... , :new_pref_key, :new_pref_val
attr_accessor ... , :new_pref_key, :new_pref_val

then added the 2 new fields on the form

<%= f.label :new_pref_key %>
<%= f.text_field :new_pref_key %>
<%= f.label :new_pref_val %>
<%= f.text_field :new_pref_val %>

on my controller i made a function that check the presence of the new fields and then merge the previous values of the prefs with new ones, like this:

#users_controller.rb
 ...
 new_key = params[:user][:preferences][:new_pref_key]
 new_val = params[:user][:preferences][:new_pref_val]
 new_preference = {
   new_key => new_val
 }
 current_params = params[:user][:preferences].merge! new_preference
...

done that i return it and pass it to the update_attributes, hope it helped!

这篇关于是否有最佳做法/一致的方式来更新包含散列键值存储的数据库字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆