0

I have a college object with a bunch of attributes (acceptance_rate, tuition...). I have an array of the attributes in "human readable format" (ex. Acceptance Rate) called @data_listing. I have a function that takes the human readable attribute and returns the actual attribute (Acceptance Rate => acceptance_rate) called get_data_attribute_name.

How can I call college.attribute with attribute being in the right format?

This is my code, but it's giving me this error: undefined method 'data_attr' for #<College:0x007fb1df017578>.

<% if flash[:notice] %>
    <div class="alert alert-success"><%= flash[:notice] %></div>
<% end %>

<div id="form">
    <%= simple_form_for :comparison, :url => '/comparison/update' do |f| %>
        <%= f.input :college, required: false, collection: ['University of Pittsburgh', 'University of Wisconsin', 'Colgate', 'Adelphi University'], :input_html => { :multiple => true } %>
        <%= f.input :section, required: false, collection: ['Professors', 'Classes', 'Difficulty', 'Campus', 'Food', 'Dorms', 'Safety', 'Social Atmosphere', 'Parties', 'Actuarial Science', 'Agriculture', 'Applied Math', 'Astronomy', 'Biology', 'Chemistry', 'Computer Science', 'Earth Science', 'Geology', 'Linguistics', 'Mathematics', 'Neuroscience', 'Physics', 'Statistics', 'Anthropology', 'Criminology', 'Economics', 'History', 'Political Science', 'Psychology', 'Sociology', 'Urban Studies', 'Women\'s Studies', 'Art', 'Classics', 'English', 'Film', 'History and Philosophy of Science', 'Literature', 'Music', 'Philosophy', 'Religious Studies', 'Theatre', 'African', 'Chinese', 'French', 'German', 'Italian', 'Japanese', 'Jewish', 'Latin American', 'Middle Eastern', 'Russian', 'Spanish', 'Other', 'Accounting', 'Finance', 'Management', 'Marketing', 'Bioengineering', 'Chemical', 'Civil', 'Computer', 'Electrical', 'Engineering Science', 'Industrial', 'Materials Science and Engineering', 'Mechanical', 'Nuclear', 'Architecture', 'Athletic Training', 'Communications/Journalism', 'Information Science', 'Nursing', 'Nutrition', 'Pharmacy', 'Public Health', 'Rehab Science', 'Social Work'], :input_html => { :multiple => true } %>
        <%= f.input :data, required: false, collection: ['Public/Private', '2-year/4-year', 'National University/Liberal Arts College', 'Academic Semester', 'Location', 'Setting', 'Endowment', 'Athletic Division', 'Nickname', '4-Year Graduation Rate', '6-Year Graduation Rate', 'Degree Types Offered', 'ROTC Offered', 'US News Ranking', 'Student-to-Faculty Ratio', 'Listing of Top-25 Graduate Programs', 'Class Sizes', 'Where Graduates Continue Study', 'Most Popular Majors', 'Undergraduate Enrollment', 'Graduate Enrollment', 'In-State/Out-of-State', 'Male/Female', '% in Fraternities', '% in Sororities', '% of Undergraduates Living On-Campus', '% of Freshman Living On-Campus', 'Ethnicity Distribution', 'In-State Tuition', 'Out-of-State Tuition', 'Room & Board', 'Average Annual Cost by Family Income Level', '% of those With Need who Got Aid', '% Who Got Need Fully Met', '% of Total Need Met', 'Scholarships/Loans', 'SAT Math', 'SAT Reading', 'SAT Writing', 'SAT Composite', 'ACT Math', 'ACT English', 'ACT Writing', 'ACT Composite', 'SAT Math Distribution', 'SAT Reading Distribution', 'SAT Writing Distribution', 'ACT Math Distribution', 'ACT English Distribution', 'ACT Composite Distribution', '% Who Submit SAT', '% Who Submit ACT', 'Acceptance Rate', '% Admitted Who Enrolled', 'Admissions Criteria', 'High School GPAs', 'High School Class Ranks'], :input_html => { :multiple => true } %>
        <%= f.button :submit, class: 'btn btn-success btn-block' %>
    <% end %>
</div>

<h1>Comparison Table</h1>
<% if !@college_listing.nil? && !@summary_listing.nil? || !@data_listing.nil? %>

<% @college_listing.shift %>
<% @summary_listing.shift %>
<% @data_listing.shift %>

<table class="table table-bordered table-striped">
    <thead>
        <th>&nbsp;</th>
        <% @college_listing.each do |college| %>
          <% @colleges.each do |college_instances| %>
            <% if college_instances.name == college %>
              <th><%= link_to "#{college}", "/#{college_instances.url}"  %></th>
            <% end %>
          <% end %>
        <% end %>
    </thead>
    <tbody>
        <% @summary_listing.each do |category| %>
            <tr>
                <th><%= category %></th>
                <% @college_listing.each do %>
                    <td>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</td>
                <% end %>
            </tr>
        <% end %>
        <% if !@summary_listing.empty? && !@data_listing.empty? %>
            <tr>
                <td>&nbsp;</td>
                <% @college_listing.each do %><td>&nbsp;</td><% end %>
            </tr>
        <% end %>
        <% @data_listing.each do |data| %>
          <% data_attr = get_data_attribute_name(data) %>
          <tr>
            <th><%= data %></th>
            <% @college_listing.each do |college_listing| %>
              <% @colleges.each do |college| %>
                <% if college.name == college_listing %>
                  <td><%= "#{college.data_attr}" %></td>
                <% end %>
              <% end %>
            <% end %>
          </tr>
        <% end %>
    </tbody>
</table>

<% end %>

1 Answer 1

1

You should use the send method.

college.send(data_attr)
Sign up to request clarification or add additional context in comments.

2 Comments

I'm confused though, doesn't the first parameter to send have to be a method? Isn't data_attr a variable that stores what the get_data_attribute_name method returns?
It doesn't have to be a method. It has to be a method's name (which of course can be in a variable)

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.