0

I would like to have review.avg_answer2, review.avg_answer3, etc outputted by the following loop:

  <% @count = 1 %>
  <% 10.times do |x| %>
    <td><%= link_to review.avg_answer1.to_i, "#" %></td>
    <% @count += 1 %>
  <% end %>   

I know there is a simple answer, but it's not hitting me.

I tried "review.avg_answer#{@count}.to_i" but of course it doesn't work.

Thanks!

2 Answers 2

1

Does it work ?

review.send("avg_answer#{@count}").to_i
Sign up to request clarification or add additional context in comments.

Comments

0

This should work for you case..
10.times.each {|x| review.send("avg_answer#{x}")} OR10.times.each {|x| review.send(:"avg_answer#{x}")}`

If you envision to pass Params to your method then you can vary it by: 10.times.each {|x| review.send("avg_answer#{x}",params1,params2)} OR 10.times.each {|x| review.send(:"avg_answer#{x}",params1,params2)}

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.