0

Is it possible for me to dynamically determine a field name or variable, e.g.

module ApplicationHelper
def current_week
  1
end

Controller
def @player = Player.find(params[:id]) 

/item/show.html.erb
<%= @player.total_for_week_{current_week} %>

This way I can update the current_week variable, either manually or based on a formula, and have it flow to all of my views. Is there a way to do this?

Thanks!

1 Answer 1

2
<%= @player.send("total_for_week_#{current_week}") %>
Sign up to request clarification or add additional context in comments.

1 Comment

This will work, but it strikes me as hackish and probably more complicated than it needs to be. Can't you just have a method on player called total_for_current_week? Then have that method reference your current_week variable directly? Part of it is a question of who should keep track of the current_week.

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.