gpt4 book ai didi

ruby-on-rails - 未定义的方法提取数组?

转载 作者:行者123 更新时间:2023-12-04 06:36:45 25 4
gpt4 key购买 nike

所以我有这个数据结构:

+[#<Folder id: 1, name: "mollitia", parent_id: nil, user_id: 1, created_at: "2014-06-27 16:00:59", updated_at: "2014-06-27 16:00:59">,
+ #<Folder id: 2, name: "porro", parent_id: 1, user_id: 1, created_at: "2014-06-27 16:00:59", updated_at: "2014-06-27 16:00:59">,
+ #<Folder id: 3, name: "omnis", parent_id: 2, user_id: 1, created_at: "2014-06-27 16:00:59", updated_at: "2014-06-27 16:00:59">]

self.ancestors 返回
我可以取它的名字吗?
def pwd
self.ancestors.pluck(:name)
end

以上结果在
undefined method `pluck' for #<Array:0x007ff4da780290>

更新

结构的真棒打印:
[
[0] #<Folder:0x007fd2946a5f18> {
:id => 3,
:name => "blanditiis",
:parent_id => 2,
:user_id => 1,
:created_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00,
:updated_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00
},
[1] #<Folder:0x007fd2946ad1c8> {
:id => 2,
:name => "neque",
:parent_id => 1,
:user_id => 1,
:created_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00,
:updated_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00
},
[2] #<Folder:0x007fd2946b80c8> {
:id => 1,
:name => "ut",
:parent_id => nil,
:user_id => 1,
:created_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00,
:updated_at => Fri, 27 Jun 2014 18:04:02 UTC +00:00
}
]

最佳答案

pluck与 rails 一起使用以更改针对数据库的关联查询。它将从 SELECT * 改变它至 SELECT [attr]哪里[attr]是 pluck 的论据。因为它是一个数组,所以你不能使用它。你应该改用普通的旧 map从 ruby 。在你的情况下,它看起来像:

def pwd
self.ancestors.map(&:name)
end

关于ruby-on-rails - 未定义的方法提取数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com