rails enum用于存储数据

http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

 

新的项目中有一个字段是展示类型,可以用下拉框去做,用string存储具体的类型字段。

尝试了一下把展示类型修改为integer,用Rails enum枚举来做。

使用枚举(整型)来存储类似于下拉框选择的这类的值, 比起直接存储类型的字符串,能减少表中存储字段的大小。

 

建表语句如下

class CreateSubjects < ActiveRecord::Migration[5.0]
  def change
    create_table :subjects do |t| 
      t.string :title, limit: 40
      t.string :sub_title, limit: 40
      t.string :categories, limit: 30
      t.string :description, limit: 500 
      t.integer :status, limit: 1
      t.integer :type, limit: 1
      t.integer :use_default_image, limit: 1
      t.integer :has_ranking_list, limit: 1

      t.timestamps
    end 
  end 
end

model中定义了enum的字段值。

class Subject < ApplicationRecord

  TYPES = [ :'变化标题描述', :'固定标题描述', :'播放器专题', :'无文案专题' ]
  enum type: TYPES

  self.inheritance_column = '_disable'
end

在console里修改当前type的值,rails会自动显示成对应的字符串

[6] pry(main)> Subject.last.update_attribute :type, 2
  Subject Load (0.2ms)  SELECT  `subjects`.* FROM `subjects` ORDER BY `subjects`.`id` DESC LIMIT 1
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE `subjects` SET `type` = 2, `updated_at` = '2016-09-13 06:06:49' WHERE `subjects`.`id` = 3
  SQL (14.3ms)  INSERT INTO `versions` (`item_type`, `item_id`, `event`, `object`, `created_at`, `object_changes`) VALUES ('Subject', 3, 'update', '{\"id\":3,\"title\":\"谢谢\",\"sub_title\":\"1\",\"categories\":\"1\",\"description\":\"1\",\"status\":null,\"type\":1,\"use_default_image\":1,\"has_ranking_list\":1,\"created_at\":\"2016-09-13T04:35:23.000Z\",\"updated_at\":\"2016-09-13T04:35:23.000Z\"}', '2016-09-13 06:06:49', '{\"type\":[1,2],\"updated_at\":[\"2016-09-13T04:35:23.000Z\",\"2016-09-13T06:06:49.000Z\"]}')
   (54.0ms)  COMMIT
=> true
[7] pry(main)> Subject.last
  Subject Load (0.4ms)  SELECT  `subjects`.* FROM `subjects` ORDER BY `subjects`.`id` DESC LIMIT 1
=> #<Subject:0x00558622d04d98
 id: 3,
 title: "谢谢",
 sub_title: "1",
 categories: "1",
 description: "1",
 status: nil,
 type: "播放器专题",
 use_default_image: 1,
 has_ranking_list: 1,
 created_at: Tue, 13 Sep 2016 12:35:23 CST +08:00,
 updated_at: Tue, 13 Sep 2016 14:06:49 CST +08:00>

而直接查询mysql会发现,这个字段里存的是2.

$ bundle exec rails db -p
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 72
Server version: 5.5.47-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from subjects;
+----+--------+-----------+------------+-------------+--------+------+-------------------+------------------+---------------------+---------------------+
| id | title  | sub_title | categories | description | status | type | use_default_image | has_ranking_list | created_at          | updated_at          |
+----+--------+-----------+------------+-------------+--------+------+-------------------+------------------+---------------------+---------------------+
|  3 | 谢谢   | 1         | 1          | 1           |   NULL |    2 |                 1 |                1 | 2016-09-13 04:35:23 | 2016-09-13 06:06:49 |
+----+--------+-----------+------------+-------------+--------+------+-------------------+------------------+---------------------+---------------------+
1 row in set (0.00 sec)

在_form中的代码如下

  <div class="form-group">
    <%= f.label :type, class: 'col-sm-2 control-label' %>

    <div class="col-sm-4">
      <%= f.select :type, Subject::TYPES %>
    </div>
  </div>

在index和show页面直接读取数据就ok了

<%= subject.type %>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注