ruby - Rails - Constants Hash? -


I need a model (?) On my app that is basically the position of the second unit, I want to store the ID, but in my app, the scene is talking about a good word description. For example, 1 = new, 2 = used etc.

How can I go about implementing it the best way which means that I can easily set and retrieve this status column without repeating myself?

I finally like something

  Foo.status = 'New' (actually set the value for 1)  

and

<<> Foo.status (Returns 'New', but store1)

Am I thinking about it correctly?

You can code a custom author's method:

  STATUS_VALUES = {1 = & gt; 'New', 2 = & gt; 'Modified', 3 = & gt; 'Deleted'} square fu atr_reader: status_if df position STATUS_VALUES [@status_id] end DEF status = (new_value) @status_id = STATUS_VALUES.invert [new_value] new_value ending  

for example , The following program:

  foo_1 = Foo.new foo_1.status = 'new' puts "status: # {foo_1.status}" "status_id: # {foo_1.status_id}" "foo_1 .status "puts = 'destroys' puts" status: # {foo_1.status} "" condition_ id: # {foo_1.status_id} "puts  

output:

  Status: New Status_ID: 1 Status: Deleted status_id: 3  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -