请选择 进入手机版 | 继续访问电脑版

技术控

    今日:83| 主题:61194
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Quick Tip: Use Enums in Rails for Mapped Values

[复制链接]
嘴角上の布丁 发表于 2016-10-7 09:11:25
368 6

Quick Tip: Use Enums in Rails for Mapped Values

Quick Tip: Use Enums in Rails for Mapped Values-1-技术控-management,different,advantage,completed,solution

   When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a call center, but I have pondered how I would accomplish this task with Rails. Thankfully, Rails has a solution known as Enums .
  When you are adding statuses to a model, you might be tempted to use strings to set the status. This makes perfect sense because different statuses tend to be named things like ‘pending’, ‘under review’, and ‘completed’.
  However, Rails encourages you to take advantage of Enums to replace these strings with integers. It seems counter-intuitive at first, but it is actually a perfect fit for the problem.
  Enums allow you to map string values to integers in the database so they can be queried by name:
  1. class CustomerCase < ApplicationRecord
  2.   enum status: [:open, :closed,:under_review, :pending]
  3. end
复制代码
Now in the Rails console, we can do the following:
  1. $ customer_case.open?
复制代码
We can even use scopes:
  1. $ CustomerCase.open
复制代码
Just so we can really practice using Enums, I highly recommend creating a small Rails App:
  1. $ rails new call_center
复制代码
Change into that directory and generate a quick scaffold with lots of attributes:
  1. $ rails generate scaffold CustomerCase title:string description:text status:integer agent:string
复制代码
  Notice that the status field is an integer .
  Migrate the database:
  1. $ rails db:migrate
复制代码
  Open up app/models/customer_case.rb and input the following:
  1. class CustomerCase < ApplicationRecord
  2.   enum status: [:open, :closed, :under_review, :pending]
  3. end
复制代码
Bear in mind that ActiveRecord will
   Now we have a small application up and running. Go ahead and create a couple of cases yourself with different attributes in the rails console ( rails c ):
  1. $ CustomerCase.create(title: "Case 1", description: "Our first case", status: :open, agent: "Me")
  2. => #<CustomerCase id: 1, title: "Case 1"...status: "open"
  3. $ CustomerCase.create(title: "Case 2", description: "Our second case", status: :pending, agent: "Me")
  4. => #<CustomerCase id: 2, title: "Case 2"...status: "pending"
复制代码
  It’s also a good idea to change the line in app/views/customer case/ form.html.erb that corresponds to the Enums so you can pick the actual statuses:
  1. ...
  2. </div>
  3. <div class="field">
  4.   <%= f.label :status %><br>
  5.   <%=  f.collection_select :status, CustomerCase.statuses.map{ |a| [a.first,a.first] },  :first, :second %>
  6. </div>
复制代码
I suggest creating as many different cases as possible and testing the different possibilities from the Rails console:
  1. $ bundle exec rails c
  2. $ cse = CustomerCase.create(title:"Case Title", description:"what has happend", status: "open" )
  3. $ cse.open?
  4. => true
  5. $ cse.closed?
  6. => false
  7. $ cse.pending?
  8. => false
  9. $ CustomerCase.open.to_a
  10. CustomerCase Load (0.1ms)  SELECT "customer_cases".* FROM "customer_cases" WHERE "customer_cases"."status" = ?  [["status", 0]]
  11. => #<ActiveRecord::Relation..
复制代码
  The last example calling the open scope shows the SQL used to query the database. The query is using 0 for the value, not open , as expected.
  You can also change the status of the case:
  1. $ customer_case.open?0
复制代码
(0.1ms) begin transaction
   SQL (1.0ms) UPDATE “customer cases” SET “status” = ?, “updated at” = ? WHERE “customer cases”.”id” = ? [[“status”, 3], [“updated at”, 2016-10-05 13:34:28 UTC], [“id”, 3]]
  (0.6ms) commit transaction
  => true
     More from this author

   
       
  • Rails: Dynamically Chain Scopes to Clean up SQL Queries   
  • Building Roman Numerals in a Day with Ruby Metaprogramming   
    Enums, when used properly, can be a great addition to creating readable code. Now that we have explored Enums along with a possible use case, I hope that it will make implementing other Enum-related features into your Rails app easier.
delihuaf 发表于 2016-10-7 13:01:25
小手一抖,积分带走。
回复 支持 反对

使用道具 举报

序言述說幸福 发表于 2016-10-8 21:20:11
本宫准楼下的继续跟帖。。。
回复 支持 反对

使用道具 举报

邓小敏 发表于 2016-10-14 12:57:19
小时候的梦想并不是要当什么科学家,幻想自己是地主家的少爷,家有良田千顷,终日不学无术,没事领着一群狗奴才上街去调戏一下良家少女……
回复 支持 反对

使用道具 举报

微暖 发表于 2016-10-21 03:42:02
楼主,闹哪样,你家里人知道么?
回复 支持 反对

使用道具 举报

thsdrhsrhs 发表于 2016-11-9 20:57:50
星期三不忍直视
回复 支持 反对

使用道具 举报

vyvianppp 发表于 2016-11-14 11:11:39
直播,握手!稍坐,楼主马上来!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表