在component表里用text类型的字段存储hash数据

(1)新建字段 ,这是migration的内容

  1. class AddHintsToComponents < ActiveRecord::Migration[5.0]
  2. def change
  3. add_column :components, :hints, :text
  4. end
  5. end

(2)controller

  1. def update
  2. if @component.update!(component_params)
  3. redirect_to @component, notice: 'Component was successfully updated.'
  4. else
  5. render :error
  6. end
  7. end
  1. def component_params
  2. params.require(:component).permit(:release_id,
  3. :remark, component_ids: [], hints: [:left, :mid, :right])
  4. end

(3)model

  1. class Component < ApplicationRecord
  2. serialize :hints
  3.  
  4. def after_initialize
  5. self.hints ||= {}
  6. end

(4)_component.html.erb

  1. <td>
  2. left:<%= component.hints["left"] %> |
  3. mid:<%= component.hints["mid"] %> |
  4. right:<%= component.hints["right"] %>
  5. </td

(5)_form.html.erb

  1. <%= f.fields_for :hints do |h| %>
  2. <div class="form-group">
  3. <%= f.label 'hints left', class: 'col-sm-2 control-label' %>
  4. <div class="col-sm-4">
  5. <%= h.text_field :left, class: 'form-control',
  6. value: (@component.hints && @component.hints.key?("left") ? @component.hints["left"] : "") %>
  7. </div>
  8. </div>
  9.  
  10. <div class="form-group">
  11. <%= f.label 'hints mid', class: 'col-sm-2 control-label' %>
  12. <div class="col-sm-4">
  13. <%= h.text_field :mid, class: 'form-control',
  14. value: (@component.hints && @component.hints.key?("mid") ? @component.hints["mid"] : "") %>
  15. </div>
  16. </div>
  17.  
  18. <div class="form-group">
  19. <%= f.label 'hints right', class: 'col-sm-2 control-label' %>
  20. <div class="col-sm-4">
  21. <%= h.text_field :right, class: 'form-control',
  22. value: (@component.hints && @component.hints.key?("right") ? @component.hints["right"] : "") %>
  23. </div>
  24. </div>
  25. <% end %>

参考资料

http://vladexologija.blogspot.com/2012/12/rails-attribute-serialization_18.html

http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

使用text存储hash类型的数据 Use text filed to store the hash map的更多相关文章

  1. 如何使用Cassandra来存储time-series类型的数据

       Cassandra非常适合存储时序类型的数据,本文我们将使用一个气象站的例子,该气象站每分钟需要存储一条温度数据. 一.方案1,每个设备占用一行      这个方案的思路就是给每个数据源创建一行 ...

  2. Redis 笔记与总结2 String 类型和 Hash 类型

    Linux 版本信息: cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息) CentOS release 6.6 (Final) ...

  3. Perl中的hash类型

    hash类型 hash类型也称为字典.关联数组.映射(map)等等,其实它们都是同一种东西:键值对.每一个Key对应一个Value. hash会将key/value散列后,按序放进hash桶.散列后的 ...

  4. Redis中Value使用hash类型的效率是普通String的两倍

    什么Redis? 点击这里 最近要开发的一个项目是分布式缓存组件,解决参数缓存高效获取的问题.参数达到了500万级别,刚刚开始了解Redis.做设计的时候考虑到Value使用哪种类型的问题? 主要面临 ...

  5. redis之Hash类型常用方法总结

    redis之Hash类型常用方法总结 格式: 存--HMGET key field [field ...] 取--HMGET key field [field ...] M:表示能取多个值,many ...

  6. Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析

    Hashtable 是一个很常见的数据结构类型,前段时间阿里的面试官说只要搞懂了HashTable,hashMap,HashSet,treeMap,treeSet这几个数据结构,阿里的数据结构面试没问 ...

  7. 直接在安装了redis的Linux机器上操作redis数据存储类型--hash类型

    一.概述:   我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和 ...

  8. 关于InnoDB存储引擎text和blob类型的优化

    我们在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,如果单表的存储空间达到了近上百G或者大几十G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们有 ...

  9. StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明本系列分享地址:http://www.cnblogs.com/tdws/p/5815735.html 上一篇文章的不合理之处,已经有所修改. 今天分 ...

随机推荐

  1. String 类相关知识

    1.常用方法 1)判断字符串是否为空 public boolean isEmpty()2)获取字符串长度 public int length()3)截取子子串 public String substr ...

  2. ectouch笔记

    1.取消配送方式验证mobile\include\apps\default\controller\FlowController.class.php 第1109行 if (isset($is_real_ ...

  3. The method below converts an array of objects to a DataTable object in C#.

    http://www.c-sharpcorner.com/blogs/dynamic-objects-conveting-into-data-table-in-c-sharp1 public stat ...

  4. 为你的Visual Studio单独设置代理服务器

    http://blog.sina.com.cn/s/blog_58c506600101tycn.html 最近,因为国内访问Visual Studio Online(微软的免费代码托管服务,以前叫Te ...

  5. struts2 拦截器和actioninvocation、PreResultListener

       Interceptor说明 Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法.而它所依赖的参数Act ...

  6. phpmyadmin 链接远程mysql

    这个只是自己的笔记 新手 不记下来以后又忘记了~ 在这以前已经给mysql设置了可以远程连接的账户 版本 phpMyAdmin-4.2.11-all-languages 解压到D盘下www   本地环 ...

  7. yum管理

    一.yum发展与作用     在linux系统维护中管理员经常遇到软件包的依赖问题,有时无法解决,比如你在安装库文件时常出现报错问题,说依赖其它软件包.由于这个问题一直困绕linux的广大爱好者,开源 ...

  8. SQL查询表字段的信息

    如题,代码: select * from information_schema.columns where table_name = 'TableName'

  9. 解决安装VS2013提示“已停止工作”问题

    新安装操作系统(win8.1),手动安装各种驱动,安装VS2013,报错,见下图: 原因:显卡驱动问题. 解决办法:卸载intel显卡驱动这碧池.(系统会自动给你适配合适的)

  10. gradle init.gradle的文件配置 使用

    init.gradle文件在build开始之前执行,所以你可以在这个文件配置一些你想预先加载的操作例如配置build日志输出.配置你的机器信息,比如jdk安装目录,配置在build时必须个人信息,比如 ...