我有一个待办事项列表,其中包含存储在同一记录中的5个任务.
Todo.task_one, Todo.task_two, etc.
Run Code Online (Sandbox Code Playgroud)
我想做的是能够循环遍历这样的字段
total_tasks = ["one", "two", "three", "four", "five"]
for tasks in total_tasks
Todo.task_#{tasks} = "text here"
end
Run Code Online (Sandbox Code Playgroud)
但是,除非我使用eval "Todo.task_#{tasks} = 'text here'"我知道的不安全,否则这不起作用.即使使用eval也不是真正的解决方案,因为我需要在视图中使用erb,所以我有点卡住了.