0

as always you are irreplaceable when it comes to help. I got a new problem.

I have two Models.

User(id: integer)
Follower(id: integer, source_user_id: integer, target_user_id: integer)

I have params[:user_id] input via get.

I need to create an array of records that contains is_following => true/false.

is_following should be true when there is a Follower record present for source_user_id = params[:user_id]

is_following should be false when there is no Follower record present for this parameter

What is the most efficient way to do this?

Thank you!

2
  • can you show some example data, the question is not that clear Commented Mar 2, 2016 at 5:22
  • I am looking for a list of users that specifies whether a particular user with params[:user_id] is following each or not. Commented Mar 2, 2016 at 5:36

3 Answers 3

2

The quick answer to add a method in your User model:

class User < ActiveRecord::Base
   has_many :followers, foreign_key: :source_user_id

   def is_following? follower_id
     follower_ids.include? follower_id.to_i
   end
end

and then:

user.is_following? params[:user_id]

The association was assumed based on your question. Please note, that this is a short answer to your question. You probably want to redesign your associations, move the method into a Decorator (depending on your use case) and maybe optimize the querying.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, however only some of your code was correct.
First of all, the answer was more intended to give you an idea, since your question had no real code snippet included to base on. Please rephrase your question with valid code snippets, to refer on.
Thank you for helping! However, it's not fair to the other user to give you the credit. He also gave a little bit.
1

Edited to reflect the clarification on what you want.

Define a helper function within the User model, called is_following:

def is_following?
  !Follower.where(source_user_id: self.id).empty?
end

Now, you can use the map method to create an array. You'll have to have an array of hashes, since users don't have any kind of attribute to store the boolean value:

User.all.map { |user| {user: user, is_following: user.is_following? } }

11 Comments

I am not looking for a list of followers. I am looking for a list of users that specifies whether a particular user following each or not.
So use the second solution
I don't understand what it does. I need a list of user objects, the second one makes a list of Follower objects.
Sorry, I don't think it's very clear what you want. Do you need a lit of users, with an attribute to say whether they're being followed?
Yes, followed by the user given at params[:user_id].
|
-1

User Model modification

has_many :followers, class_name: 'Follower', foreign_key: :target_user_id

def is_followed? follower_id
    followers.where(:source_user_id => follower_id).exists?
end

And API

users = users.map {|user| { user: user, is_following: user.is_followed?(params[:user_id]) } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.