我使用 select id, parent_id....
从数据库中获取了一堆记录,并使用 map.do
循环遍历它们,在每一行上创建一个新索引.
records = results.map do |row|
row['childs'] = {}
comments[row['id']] = row
end
这在从命令行调用时工作正常,但在放置在 Rails 上下文中时出现以下错误:
>ActiveModel::MissingAttributeError in CommentsController#index
can't write unknown attribute `childs'
知道这在 Ruby 的命令行而不是 Rails 中会起作用吗?关于 Rails 的工作原理,我缺少什么?
最佳答案
row
不是散列,而是一个 ActiveModel
对象实例,因此您不能像散列的键一样向它添加/分配新属性。在您的模型中,您可以使用 attr_accessor
为 childs
属性启动 setters(写入)和 getters(读取)。
关于ruby-on-rails - 在 Rails Controller 中,在 'map.do' 循环中创建新索引失败,但在 Ruby 中没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36159481/