What is the underlying code for TableA.create(TableB.all.map(&:attributes))? [duplicate]

有些话、适合烂在心里 提交于 2019-12-25 02:24:07

问题


For example, if I use rename method in mongo ruby driver, I can check the code here

What exactly is happening when I am using map(&:attributes)? I think this means tags.map(&:attributes.to_proc).join(' '), but I am not sure why I am getting "undefined method `each_pair' for Arrayxxxxx" error with this command:

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

Any insight will be appreciated


回答1:


map returns an array of whatever is returned by the method call.

so

TableB.all.map(&:attributes)

is basically an array of

[TableB.all[0].attributes,TableB.all[1].attributes,TableB.all[2].attributes,...]

Do you want something like

TableB.all.map(&:attributes).each do |attr|
  TableA.create(attr)
end


来源:https://stackoverflow.com/questions/7708087/what-is-the-underlying-code-for-tablea-createtableb-all-mapattributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!