Loading...

Use Enums in Rails for Mapped Values

View: 506    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

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:

class CustomerCase < ApplicationRecord
  enum status: "open, :closed,:under_review, :pending]
end

Now in the Rails console, we can do the following:

$ customer_case.open?

We can even use scopes:

$ CustomerCase.open

Just so we can really practice using Enums, I highly recommend creating a small Rails App:

$ rails new call_center

Change into that directory and generate a quick scaffold with lots of attributes:

$ rails generate scaffold CustomerCase title:string description:text status:integer agent:string

Notice that the status field is an integer.

Migrate the database:

$ rails db:migrate

Open up app/models/customer_case.rb and input the following:

class CustomerCase < ApplicationRecord
  enum status: "open, :closed, :under_review, :pending]
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):

$ CustomerCase.create(title: "Case 1", description: "Our first case", status: :open, agent: "Me")
=> #<CustomerCase id: 1, title: "Case 1"...status: "open"
$ CustomerCase.create(title: "Case 2", description: "Our second case", status: :pending, agent: "Me")
=> #<CustomerCase id: 2, title: "Case 2"...status: "pending"

It’s also a good idea to change the line in app/views/customercase/form.html.erb that corresponds to the Enums so you can pick the actual statuses:

...
</div>
<div class="field">
  <%= f.label :status %><br>
  <%=  f.collection_select :status, CustomerCase.statuses.map{ |a| [a.first,a.first] },  :first, :second %>
</div>

I suggest creating as many different cases as possible and testing the different possibilities from the Rails console:

$ 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]]
=> #<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, notopen, as expected.

You can also change the status of the case:

$ cse.pending!

0.1ms) begin transaction
SQL (1.0ms) UPDATE “customercases” SET “status” = ?, “updatedat” = ? WHERE “customercases”.”id” = ? [[“status”, 3], [“updatedat”, 2016-10-05 13:34:28 UTC], [“id”, 3]]
(0.6ms) commit transaction

Use Enums in Rails for Mapped Values

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.

Posted on 07-10-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Twitter-like Hashtag Function in PHP

    Twitter-like Hashtag Function in PHP

    View: 0    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    If you ever want a function to hashtag and style words within a string which has '#' next to it like twitter. This piece of code will help

  • PSITS Automated Voting System

    PSITS Automated Voting System

    View: 24    Download: 3   Comment: 0

    Category: Php&mySql     Fields: none

    A free sourcecode for PSITS Automated Voting System develop in PHP programming language. The purposed of the system is to automate the process of voting and maintain the quality of data

  • PHP-University Application System

    PHP-University Application System

    View: 30    Download: 6   Comment: 0

    Category: Php&mySql     Fields: none

    Greeting from Malawi the warm heart of Africa. I developed this university registration system project just to share with the people who can make use of the project like this and at the same time for others like students who can learn from it. This...

  • School Event Management System in PHP/MSQLi

    School Event Management System in PHP/MSQLi

    View: 22    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This School Event Management System can create school events such as Volleyball games, Basketball, Cultural presentation, Election of school officers etc . During school election Instead of having a compile list of candidates and voters this system...

  • Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    View: 27    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This reservation system has the ability to help its customers find available rooms, cottages and even function hall for their convenience . And in here, they will also have the idea of the room rates where they can quickly reserve for their family...

  • Activity log

    Activity log

    View: 20    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Simple program to track user's activity log-in time and online/offline status.

  • Electricks - eCommerce

    Electricks - eCommerce

    View: 22    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Electricks is an e-commerce website built using PHP, Bootstrap and some jQuery plugins. It has all the major functionalities of a typical e-shop. This project was used to present in Southern Luzon State University - Lucban. Feel free to use this code...

  • Simple Email App Using PHP

    Simple Email App Using PHP

    View: 18    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Creating a simple PHP script using mail function for sending out emails.

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd