Search code examples
ruby-on-railsdynamicglobal-variablesinstance-variables

creating dynamic variable for ruby objects in rails project


I am fairly new to rails and am just getting into using cool gems and APIs. I have been made aware that this community values the contents of questions and answers, or it is expected that they are constructed in a specific way. The short way to ask my question and the long way are provided. Please let me know which is preferred here! It is a real question though!

SHORT VERSION

I have a users_controller and User object with a username attribute in a rails app. How do I create global variables for these users that is dynamically based on their username? Example: I want with my user (id = 1, first_name = "Rob", username = "rocky") to be callable as @rocky. So what would go below in my Users_controller that is based on the first code line below working for me in terminal:

@rocky = User.find_by_username("rocky")

WHATGOESHERE = User.find_by_username(params[:username]} 

or should I be using this in some shape or form in place of params[:username]

@"#{user.username}"

Below is the longer version of my question. It is more detailed and follows more closely how I approached the issue. The first one... that I wrote second, is more concise but that's not always what people want... please let me know which is preferred on this site. Thanks!!

LONG VERSION

I need some clarity on a few things. I am using a gem called "has_friendship" to create friendships between my users (link to gem- https://github.com/sungwoncho/has_friendship ).

First, this is the documentations example of how to request a friendship, starting with the creation of the users.

@mac = User.create(name: "Mac")
@dee = User.create(name: "Dee")

# @mac sends a friend request to @dee
@mac.friend_request(@dee)

This is where I first became confused. My users don't have a "name" field. But that's ok. I managed to figure out that I just need to assign my created users global variables as they do... since my users will be interacting with each other behind their "username" attribute. So first question, How do I assign a dynamic variable name to each user? In the documentation, they are hard-coding in the names "Mac" and "Dee." I need to have this global variable be created upon the creation of the object.. So my plan is to do this in the controller. Here I am already defining @users and @user in users#show

@users = User.all
@user = User.includes(:wallet).find_by_id(params[:id])

So my thought process is that the left side of the equation should be the name of what you're naming and the right side is what that name is referring to. So for the right side, I'd think to put

User.find_by_username(params[:username])

as when in the terminal, if I replace the content in parenthesis with an actual username in quotes, it brings up that user's info. So how do I write the left side. I would think the left side is something like this:

@"#{params[:username]}"

So in full I currently have the following in my users_controller to assign global variables to my users based on their username atttribute...

@"#{params[:username]}" = User.find_by_username(params[:username])

This, especially the left side, does not look at all right to me. So I've looked around on google a bunch and the only other thing I can find that looks like the right way to do this is by using "instance_variable_set" but everything I've looked at doesn't make total sense for my situation... (as usual.. ha)


Solution

  • Ok what I get to know from your question is you want to use friend_request method to associate two users.

    For this, you don't need to assign them to any variables. You can directly do this by something like this - Suppose there are two user's

    id=1 first_name='Rocky' username='rocky'

    id=2 first_name='Nimish' username='nimish'

    User.find_by(username: 'rocky').friend_request(User.find_by(username: 'nimish'))
    

    OR

    User.find_by_username('rocky').friend_request(User.find_by_username( 'nimish'))
    

    Also, If you want to assign them to instance variable then it is not necessary to create an instance variable corresponding to the username value You can simply assign them to @user and @requested_user and then

    @user.friend_request(@requested_user)