0

Hi can someone tell me what went wrong here?

@organizations_of_user = user.memberships.verified.pluck :organization_id
@organizations_of_user << nil
@permitted_category_ids= Category.where(......, organization_id:@organizations_of_user)

seems like the @organizations_of_user has some problems. This code is written by one of my previous colleague. I don't know what exactly this << nil is doing, but somehow, without this, the code crashes. But with this, the Category scope will only display organization_id : nil.

does someone have any idea?

Thanks a lot!

MangooSaSa
  • 3,411
  • 3
  • 14
  • 16

1 Answers1

0

@organizations_of_user is an array

@organizations_of_user = user.memberships.verified.pluck(:organization_id) #Extracts organization_ids in an array [1,2,3...]
@organizations_of_user << nil # Adds nil to the array [1,2,3,nil...]
@permitted_category_ids= Category.where(......, organization_id:@organizations_of_user) #Search for Categories with organization_ids inside the array [1,2,3,nil...]. (nil extracts Categories without an organization_id)
Erick Eduardo Garcia
  • 1,147
  • 13
  • 17
  • thanks for the explanation, that's really helpful!! looks like `user.memberships.verified.pluck(:organization_id)` returns nothing. I don't know what's wrong with the user.memberships. Normally, it's just `user.memberships.pluck`, I'm not sure what's the `verified` is doing here, or something wrong with the memberships policy? – MangooSaSa Jul 17 '15 at 10:01
  • I check the code of membership: `validates_uniqueness_of :user_id, scope: :organization_id scope :verified, -> {where("verified_at IS NOT NULL")}` the attribute `verified_at` is null all the time, which is not created properly. Thanks for the help, Erick! – MangooSaSa Jul 17 '15 at 11:24