[英]In a Rails controller creating a new index in a 'map.do' loop fails, yet it's fine in Ruby
I have a bunch of records from a database using select id, parent_id....
and loop over them using map.do
creating a new index on each row. 我从数据库中使用
select id, parent_id....
获得了一堆记录select id, parent_id....
并使用map.do
在每行上创建一个新索引来map.do
它们。
records = results.map do |row|
row['childs'] = {}
comments[row['id']] = row
end
This works fine when called from the command-line but when placed in the Rails context I get the following error: 从命令行调用时,此方法工作正常,但是当置于Rails上下文中时,出现以下错误:
>ActiveModel::MissingAttributeError in CommentsController#index
can't write unknown attribute `childs'
Any idea what this would work in Ruby at the command line and not Rails? 知道这在Ruby而不是Rails的命令行中能做什么吗? What am I missing about how Rails works?
我对Rails的工作方式缺少什么?
row
is not a hash, but an ActiveModel
object instance, so you can't just add/assign new attributes to it like a key to the hash. row
不是哈希,而是ActiveModel
对象实例,因此您不能像哈希键一样向其添加/分配新属性。 In your model, you can use attr_accessor
to initiate setters(writing) and getters(reading) for childs
attribute. 在模型中,您可以使用
attr_accessor
来初始化childs
属性的setter(写入)和getter(读取)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.