Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:26:56+00:00 2026-06-15T18:26:56+00:00

I am trying to return a variable with values of an array of users.

  • 0

I am trying to return a variable with values of an array of users. There are a few conditions that must be met. The user must have public_find set to true, it must not be the current_user (session variable of the user currently logged in), and it must not already be part of a friendship. The first and second conditions work perfectly. However, I am having an issue with the third part where the current_users.friendships needs to be an array of the ID values of the users where the association already exists. Any thoughts?

@users = User.find(:all, :conditions => ['
    public_find=true AND 
    id <> ? AND 
    id NOT IN (?)',
    current_user.id, current_user.friendships])

Edit:

I’ve figured out that I was missing pluck from the list. This works good now. However, if someone does not yet have a friend then current_user.friendships.pluck(:friend_id) will return NULL. I know that it is bad practice and unexpected results returned when using NOT IN and NULL. However, how do you create a condition where you can set the value to something realistic like [0] or [1] if the array returned is empty?

@users = User.find(:all, :conditions => ['
    public_find=true AND 
    id <> ? AND 
    id NOT IN (?)',
    current_user.id, current_user.friendships.pluck(:friend_id) || [0]])

EDIT AGAIN:

I got it working. However, now I want to know if this is best practice to have a statement like this. It basically is doing a check to see if the current_user.friendships.pluck(:friend_id) is empty or not. If it is then return [0]. Otherwise return an array of the user ids (foreign keyed as friend_id).

@users = User.find(:all, :conditions => ['
    public_find=true AND 
    id <> ? AND 
    id NOT IN (?)',
    current_user.id, 
    (current_user.friendships.pluck(:friend_id).empty? ? [0] : current_user.friendships.pluck(:friend_id))])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:26:57+00:00Added an answer on June 15, 2026 at 6:26 pm

    You can write this a little nicer ..

    Show all users where public_find is true and also exclude the currently logged in user or any of their friends

    ids = current_user.friendships.map(&:friend_id).concat([current_user.id])
    @users = User.where(:public_find => true).where('id not in ?', ids) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to return all of the names that have been provided to a
I'm trying to make a SPARQL query, that returns a variable which contains two
Im trying to return the value that a $ajax call returns, from a function
Im trying to return a SimpleQuery list that queries a single table and uses
I'm trying to return a left child as a pointer I have template <typename
I am trying to return the values of the SECTION attribute of the INTRO
I'm trying to bind a php variable to pl/sql array. The pl/sql procedure works
I'm trying to access a variable in a smarty array. The array is called
I have a class that is basically a wrapper to an Array and implements
In my application I have the following relationship: Document has_and_belongs_to_many Users User has_and_belongs_to_many Documents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.