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 8174535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:29:04+00:00 2026-06-06T22:29:04+00:00

How do I make a dynamic variable name/object? I have a polymorphic model for

  • 0

How do I make a dynamic variable name/object?

I have a polymorphic model for sending request and joining models together upon approval.
Example a user can join a company, project or group ect.

so I have a profiles model that belongs_to various models, but I only want the relationship to be built once a request is accepted.

profile.rb

class Profile < ActiveRecord::Base
    belongs_to :user
    belongs_to :company
    has_many :requests
    has_many :requested, as: :requestable

    attr_accessible :first_name, :last_name


    validates :first_name, presence: true
    validates :last_name, presence: true

end

I need my controller to be able to apply its actions based on the model that it is dealing with.

I was hoping that with @profile.@belongs_to would be the same as @profile.company which in this case would be nill. Then from there @profile.@belongs_to = @requestable

because the profile is always going to belong_to the model it joins @belongs_to will always be the model name in lowercase.

I have been messing around with the contents of objects posted to a flash message, to try and figure this out.

requests_controller.rb

class RequestsController < ApplicationController
 before_filter :load_requestable
     def accept
        @request = Request.find(params[:id])
        @profile = Profile.find(@request.profile.id)

        redirect_to [@requestable, :requests], notice: "#{@profile.@belongs_to} #{@request.profile.first_name} #{@request.profile.last_name} id: #{@request.profile.id} wants to join #{@requestable.name}  id: #{@requestable.id}"
     end

     private

     def load_requestable
       klass = [Company, Profile].detect { |c| params["#{c.name.underscore}_id"]}
       @requestable = klass.find(params["#{klass.name.underscore}_id"])
       @belongs_to = klass.to_s.downcase
    end
 end

I have played around in the console with somthing along the lines of:

profile = Profile.first
profile.company = Company.first 

and this creates the join in the object which can then be saved.

  • 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-06T22:29:05+00:00Added an answer on June 6, 2026 at 10:29 pm

    If @belongs_to contains your association you can simple call @profile.send(@belongs_to) or in case of an assignment @profile.send("#{@belongs_to}=",@requestable)

    #send allows you to send any message to any object in ruby. Think dynamic method invocation.

    And you should be done

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set dynamic variable names. such as function make(name) local name..bar =
I have to make dynamic hashes, so the class example won't work since the
I have a method that will provide an array of model object. Some of
I'm currently using Generics to make some dynamic methods, like creating an object and
I have a variable of type Dynamic and I know for sure one of
I am wondering whether it is possible to make dynamic variables in Java. In
I'm trying to make dynamic menu using xml data, I'm creating that menu using
I want to make Dynamic UI for a desktop application. I am planning to
I tried to make a dynamic SQL which gets all tasks for some users
i want to make a dynamic list with buttons on it. My button in

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.