0

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?

1
  • I think you can do f.select :priority, Task::PRIORITIES Commented Oct 23, 2016 at 18:05

2 Answers 2

1

Try using enums for this, like this

class Task < ApplicationRecord
    belongs_to :user
    enum priority: { later: 1, next: 2, now: 3}
 end
Sign up to request clarification or add additional context in comments.

3 Comments

how should I change my form after this? It works, but I need a dropdown menu in my simple form
Ok, finally I did it. Thanks a lot :)
Sorry I missed your first comment, I'm glad you got it working though.
0

To create a dropdown menu with an enum, you need something like this:

<%= f.select :priority, Effort.priorities.keys.map { |priority| [priority.titleize, priority] }, {prompt: true}, {class: "dropdown-select-field"} %>

As a bonus for using enum, you can call, for example:

Task.low on the class, or task.next? on an instance.

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.