如何在rails列表中保留哈希值 [英] how to keep a hash value in the table column in rails

查看:99
本文介绍了如何在rails列表中保留哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对rails很陌生。我有一张表,其中包含所有用户的基本信息,例如
$ b

用户(id,姓名,电子邮件)现在我正在尝试请将这些用户的其他信息保存在一个单独的表格中,例如

user_details(id,user_id,additional_info)

我已经创建了这个user_details表。现在我尝试保留一些默认记录以供检查。如何将附加信息的散列值保存在additional_info中,就像我在User_details表的additional_info列中的其他详细信息是 user's(指定,薪水,prjtname)一样。



我还必须为 User_details 表的 additional_info列提供数据类型。请给出一些建议。

解决方案

该列的数据类型必须是文本,因此可以在您的迁移中指定: p>

  add_column:user_details,:additional_info,:text 

然后在你的模型中,你必须指定这个列将包含一个散列,并且你使用serialize命令来做到这一点:

  class UserDetail< ActiveRecord :: Base 
serialize:additional_info

之后,您可以保存哈希信息。 p>

  @ud = UserDetail.new 
@ ud.additional_info = {:salary => 1000000}
@ ud.save


i am very new to rails . I am having a table holding all users basic information like

users(id,name,email) now i am trying to keep some additional information of these users in a separate table like

user_details(id,user_id,additional_info)

I have created this user_details table . Now i try to keep some default records for checking . how to keep the hash value of the user's additional details in the additional_info like if my additional details are user's (designation,salary,prjtname) in the column additional_info of the User_details table.

Also what datatype i have to give for this additional_info column of the User_details table. Please give some suggestions ..

解决方案

The data type for that column must be text so in your migrations you can specify:

add_column :user_details, :additional_info, :text

Then in your model you have to specify that this column will contain a hash and you do that with the serialize command:

class UserDetail < ActiveRecord::Base
  serialize :additional_info

After that you can save hash information.

@ud = UserDetail.new
@ud.additional_info = {:salary => 1000000}
@ud.save

这篇关于如何在rails列表中保留哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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