title: Rails model instance with dynamic attribute value based on relationship
body: <p>My AdminUser creates a survey and the questions that go along with it.  A different User then takes the survey.  A survey will be taken by multiple Users.  Some of the questions in the survey, will need to include an attribute of that User (example: name).  So the AdminUser would create a question like "What did USER eat?" but the User who takes the survey will see "What did Jane eat?". </p>
  
  <p>Currently, I have a Survey model and a Question model.  I would like the questions in the survey to include the user's name that is taking the survey.  As an example, if the question is "What do you eat?" I would like it to say "What does Jan eat?"</p>
  
  <p>To temporarily accomplish this I created a method on my Survey model that creates new Questions each time a new survey is created.  This is clogging up my database with ultimately the same questions with just 1 word difference.  It also makes it more difficult to link different users' answers to the same question since each question instance is unique.</p>
  
  <p>Here is my current Survey model</p>
  
  <p><strong>survey.rb</strong></p>
  
  <pre><code>class Survey &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :surveyable, :polymorphic =&gt; true
    has_many :questions, :dependent =&gt; :destroy
  
  
    def self.generate_for_user(survey_user)
        new_survey = Survey.new
        new_survey.surveyable_id = survey_user.id
        new_survey.surveyable_type = "User"
        new_survey.user = client
        new_survey.save
        Question.new(position: 1, tier: 1, content: "When did you first become aware of #{survey_user.first_name}'s needs?", question_type: "Response", survey_group: "User", survey_group_question_id: 1, choices: "", survey_id: new_survey.id).save
  
        Question.new(position: 2, tier: 1, content: "What foods does #{survey_user.first_name} regularly need to avoid?", question_type: "Response", survey_group: "User", survey_group_question_id: 2, choices: "", survey_id: new_survey.id).save
  
       ... and so on ...
  
  end
  </code></pre>
  
  <p>Here is my current Question model</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
    belongs_to :survey
  end
  </code></pre>
  
  <p>As you can see, I really dont think I went about this the right way.  </p>
  
  <p>How can I create Questions that can have a dynamic attribute that is based on one of its relationships?  Is that possible?  </p>
  
  <p>Do I need the attribute to be static with a placeholder and then call a method on the question that inserts the dynamic name into the placeholder when its called?</p>
  
  <p>I would like to be able to call</p>
  
  <pre><code>Question.new(content: "What does #{survey_user_name} eat?")
  Question.new(content: "What does #{survey_user_name} wear?")
  </code></pre>
  
  <p>And I want the #{name} to be replaced with the survey owner.  So the same question can be applied to many different surveys and still use the specific survey owners name.</p>