Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method pluck for array?

So I have this data stucture:

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

which is returned by self.ancestors can I pluck its names?

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

The above results in

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

Update

Awesome Print of the 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
    }
]
like image 623
Starkers Avatar asked Jun 27 '14 18:06

Starkers


1 Answers

pluck is used with rails to change the association query against the database. It will change it from SELECT * to SELECT [attr] where [attr] is the argument to pluck. Since it's an array, you can't use it. You should instead use regular old map from ruby. In your case, it would look something like:

def pwd
  self.ancestors.map(&:name)
end
like image 80
kddeisz Avatar answered Sep 23 '22 11:09

kddeisz