railsbook icon indicating copy to clipboard operation
railsbook copied to clipboard

undefined method `pluck' for []:Array

Open joao-j-souza opened this issue 12 years ago • 7 comments

I'm trying to implement it in one app but I got this error msg: " undefined method `pluck' for []:Array", I think that this is in this query: "followees_ids = current_user.followees(User).pluck(:id)". I'll be grateful if anyone help me in solve this issues.

joao-j-souza avatar Aug 16 '13 15:08 joao-j-souza

followees_ids = current_user.followees(User).pluck(:id) Looks like the current_user has not followed anyone, there should be a small check, have this method in your controller and remove that line

def followees_ids
current_user.followees(User) && current_user.followees(User).pluck(:id)
end

vysakh0 avatar Aug 16 '13 16:08 vysakh0

Hi Vysakh, thanks for the reply! I did as you mentined but the issue continues, now in the function. In the console, when I try it: current_user.followees(User) is return [].

joao-j-souza avatar Aug 16 '13 18:08 joao-j-souza

That is because the current_user is not following anyone. When a user first signs up he/she is not following anyone, so current_user.followees(User) is empty, subsequently your news feed is empty. If you want that to return some value, you need to follow another user in your app, then the array will not be empty :)

vysakh0 avatar Aug 16 '13 19:08 vysakh0

Sorry, I'm nb. I'm trying do that....

if followees_ids @activities = PublicActivity::Activity.where(owner_id: followees_ids, owner_type: "User")
end

...

def followees_ids current_user.followees(User) && current_user.followees(User).pluck(:id) end

But still got a msg: "undefined method `pluck' for []:Array"

How could I solve that?

joao-j-souza avatar Aug 19 '13 18:08 joao-j-souza

Go to rails console and check these, temme what output you get

user = User.first
user.followees(User)

Also, could you push your code in github so that I could see?

vysakh0 avatar Aug 19 '13 19:08 vysakh0

It return [] in console. I think that it solve the problem... unless current_user.followees(User).empty? followees_ids = current_user.followees(User).pluck(:id) end

joao-j-souza avatar Aug 20 '13 14:08 joao-j-souza

hai @joao32 this is very late message .. Try this if you are using Rails 4 followees_ids = current_user.followees(User).map(&:id)

rinoldsimon avatar Aug 04 '14 17:08 rinoldsimon