我正在尝试遍历由空格分隔的数组:
diff_attr = []
%w[name hours].each do |a|
if @old_place.a != new_place[a.to_sym]
diff_attr << a
end
end
我想比较两个不同对象的属性(名称和时间)。现在,我收到一个未定义的方法“a”错误。现在,a 被评估为@old_place 的方法而不是变量。
如何计算 block 内的迭代器以便进行比较:
@old.place.name != new_place[:name]
代替
@old.place.a != new_place[:a]
最佳答案
好吧,您可以像为 new_place
那样做(假设两者都是 ActiveRecord 对象):
if @old_place[a] != new_place[a]
或者,使用Object#send
:
if @old_place.send(a) != new_place.send(a)
关于ruby-on-rails - 评估 Controller 中每个 do block 内的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12867481/