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

  • SEARCH
  • Home
  • 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 777439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:36:24+00:00 2026-05-14T19:36:24+00:00

I have rails app which has a list of users. I have different relations

  • 0

I have rails app which has a list of users. I have different relations between users, for example worked with, friend, preferred. When listing the users i have to decide if the current user can add a specific user to his friends.

     -if current_user.can_request_friendship_with(user)
      =add_to_friends(user)
    -else
      =remove_from_friends(user)

   -if current_user.can_request_worked_with(user)
      =add_to_worked_with(user)
    -else
      =remove_from_worked_with(user)

The can_request_friendship_with(user) looks like:

  def can_request_friendship_with(user)
   !self.eql?(user) && !self.friendships.find_by_friend_id(user)
  end

My problem is that this means in my case 4 query per user. Listing 10 users means 40 query. Could i somehow eager load this?

  • 1 1 Answer
  • 1 View
  • 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-05-14T19:36:24+00:00Added an answer on May 14, 2026 at 7:36 pm

    Let’s we have got random_users and current_user
    so non_firends = random_users - current_user.freindships will return in two queries all non_friends from users query.

    Actually you can preload all your friends and use include? method for friendships Array:

    @friendships = current_user.friendships
    
    def can_request_friendship_with(user)
      !self.eql?(user) && @friendships.include? user
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 3.1.3 app which uses devise for users authentication and soft-deletes
I have a rails app which requires users to verify that they own a
I have RailsAdmin running for my Rails 3 app which has two models -
I have a Rails app which has 2 databases. Legacy DB with table called
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
In my Rails 3 app, I have a list of questions. Each question has
I have a rails app which currently has 3 models: Post , User and
I have a Rails web app which has static files under dynamic restful urls.
I am working on a rails 3 app which has different languages in my
I have a rails app which has data in MySQL db. I am using

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.