Undefined pluck method for an array?

So, I have this data structure:

+[#<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">]

who comes back self.ancestors can I rip off his names?

def pwd
    self.ancestors.pluck(:name)
end

Above results in

undefined method `pluck' for #<Array:0x007ff4da780290>

Update

Awesome Printing Structure:

[
    [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
    }
]
+4
source share
1 answer

pluckused with rails to change the association request to the database. He will change it from SELECT *to SELECT [attr], where [attr]is the argument to be torn out. Since this is an array, you cannot use it. Instead, you should use the plain old mapfrom ruby. In your case, it will look something like this:

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

Source: https://habr.com/ru/post/1546349/


All Articles