4

I have a Hotels table in my database, and one of the columns is :status (integer). I'm looking to convert these integers into strings, so 1 = "Awaiting Contract", 2 = "Designing" and so on...

I have searched Stack for some answers, and the lack of them makes me think that I'm coming at this problem from the wrong angle? I used to do this in PHP whilst pulling the data. New-ish to Rails so any help, or best practise advice would be much appreciated.

2 Answers 2

11

Check enum of ActiveRecord - doc.

Here you can configure your :status:

class Hotel < ActiveRecord::Base
  enum status: { waiting_contract: 1, designing: 2 }

  def format_status
    status.to_s.humanize
  end
end

It'll create methods like this:

hotel.waiting_contract?
hotel.designing?

hotel.waiting_contract!
hotel.format_status # => "Waiting contract"

Hope that helps!

UPDATE

Similar functionality might be achieved by overriding the status method itself, although having separate methods is more advised:

class Hotel < ActiveRecord::Base
  enum status: { waiting_contract: 1, designing: 2 }

  def status
    super.to_s.humanize
  end
end

Furthermore, decorators are something you should look into for view-specific methods.

Sign up to request clarification or add additional context in comments.

6 Comments

What would be the "better looking" string way? Add a method to the model that returns strings based on the status codes?
if hotel status is "waiting contract" (hotel.waiting_contract!), when asked for status "waiting_contract" will be returned - hotel.status # => "waiting_contract", but in templates "Waiting Contract" would be expected... I'll update answer with some suggestion to this
@PawełDawczak, it would be a good idea to mention the possiblity to override status using super.to_s.humanize in the new method's body. although, the wiser option would be indeed to have separate methods. and to shift view-specific methods to decorators.
@Humza - I did not recommend overwriting "original" status only because of personal preference (experience?). In my code I'd rather avoid this approach (what if in future I would need "original" output of status method? I'd break every occurrence in my current code). However, it's worth mentioning it is possible and completely valid code!
@PawełDawczak Agree with you completely but felt it necessary to point out the possibility to the OP
|
0

It depends what you need the list for. An alternative to the above ideas, is to create a hash. Hashes are very Ruby and designed just for this sort of paired data.

Create the hash, (enumeration typing is automatic.)

   my_h = { "waiting" => 1, "design" => 2 }

Then to access

my_h["waiting"] = 1

There's much more you can do with hashes. This is just the simplest case.

A hash may or not fulfill your needs, but it's a splendid tool that comes with a nice set of Ruby worker methods.

http://ruby-doc.org/core-2.2.0/Hash.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.