TableA.create的底层代码是什么(TableB.all.map(&:attributes))?

时间:2011-10-10 02:49:03

标签: ruby-on-rails ruby copy

例如,如果我在mongo ruby​​驱动程序中使用重命名方法,我可以查看code here

当我使用map(&:attributes)时到底发生了什么? 我认为这意味着tags.map(&:attributes.to_proc).join(''),但我不确定为什么我得到“未定义的方法`each_pair'for Arrayxxxxx”错误使用此命令:

TableA.create(TableB.all.map(&:attributes))

任何见解都将受到赞赏

1 个答案:

答案 0 :(得分:2)

map返回方法调用返回的任何数组。

所以

TableB.all.map(&:attributes)

基本上是

的数组
[TableB.all[0].attributes,TableB.all[1].attributes,TableB.all[2].attributes,...]

你想要像

这样的东西
TableB.all.map(&:attributes).each do |attr|
  TableA.create(attr)
end