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