Damir Nurgaliev Damir Nurgaliev - 1 year ago 143
Ruby Question

Converting integer type to string rails

i'm using task model, which has 3 fields: title:string, completed:boolean, priority:integer
I want to transform integer values(1,2,3) to string values(Next,Now,Later) in priority.
I wrote to model:

class Task < ApplicationRecord
belongs_to :user
['Later', 1],
['Next', 2],

And also in form:

= f.input :priority, Task::PRIORITIES

Everything should work but I get an error:no implicit conversion of Symbol into Integer in this line. How can I fix it?

Answer Source

Try using enums for this, like this

class Task < ApplicationRecord
    belongs_to :user
    enum priority: { later: 1, next: 2, now: 3}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download