Damir Nurgaliev Damir Nurgaliev - 1 month ago 11
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
PRIORITIES = [
['Later', 1],
['Next', 2],
['Now',3]
]


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

Try using enums for this, like this

class Task < ApplicationRecord
    belongs_to :user
    enum priority: { later: 1, next: 2, now: 3}
 end