【问题标题】:What is the underlying code for TableA.create(TableB.all.map(&:attributes))? [duplicate]TableA.create(TableB.all.map(&:attributes)) 的底层代码是什么? [复制]
【发布时间】:2011-10-10 02:49:02
【问题描述】:

例如,如果我在mongo ruby​​驱动中使用rename方法,我可以检查code here

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

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

任何见解将不胜感激

【问题讨论】:

  • 不是重复的,但感谢您的指点,我会检查所有这些链接。我只是想了解为什么'each_pair'..

标签: ruby-on-rails ruby copy


【解决方案1】:

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

【讨论】:

  • 谢谢你,橄榄!你帮我解决了一个非常困难的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2013-07-29
  • 2021-09-17
  • 2017-07-26
相关资源
最近更新 更多