技术控

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

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

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

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

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:
  [code]class CustomerCase < ApplicationRecord
  enum status: [:open, :closed,:under_review, :pending]
end[/code]  Now in the Rails console, we can do the following:
  [code]$ customer_case.open?[/code]  We can even use scopes:
  [code]$ CustomerCase.open[/code]  Just so we can really practice using Enums, I highly recommend creating a small Rails App:
  [code]$ rails new call_center[/code]  Change into that directory and generate a quick scaffold with lots of attributes:
  [code]$ rails generate scaffold CustomerCase title:string description:text status:integer agent:string[/code]   Notice that the status field is an integer .
  Migrate the database:
  [code]$ rails db:migrate[/code]   Open up app/models/customer_case.rb and input the following:
  [code]class CustomerCase < ApplicationRecord
  enum status: [:open, :closed, :under_review, :pending]
end[/code]  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 ):
  [code]$ CustomerCase.create(title: "Case 1", description: "Our first case", status: :open, agent: "Me")
=> # $ CustomerCase.create(title: "Case 2", description: "Our second case", status: :pending, agent: "Me")
=> # app/views/customer case/ form.html.erb that corresponds to the Enums so you can pick the actual statuses:
  [code]...


  <%= f.label :status %>

  <%=  f.collection_select :status, CustomerCase.statuses.map{ |a| [a.first,a.first] },  :first, :second %>
[/code]  I suggest creating as many different cases as possible and testing the different possibilities from the Rails console:
  [code]$ bundle exec rails c
$ cse = CustomerCase.create(title:"Case Title", description:"what has happend", status: "open" )
$ cse.open?
=> true
$ cse.closed?
=> false
$ cse.pending?
=> false
$ CustomerCase.open.to_a
CustomerCase Load (0.1ms)  SELECT "customer_cases".* FROM "customer_cases" WHERE "customer_cases"."status" = ?  [["status", 0]]
=> #   You can also change the status of the case:
  [code]$ customer_case.open?0[/code]  (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.
友荐云推荐




上一篇:深入理解 iOS 开发中的锁
下一篇:Creating A Bootstrap Admin Template With Charts
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

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
直播,握手!稍坐,楼主马上来!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表