Search code examples
ruby-on-railsruby-on-rails-4collection-select

Call function for Rails colleciton_select


I need to better manipulate a strong for a collection_select. I only see how to call a function on the object, but I need to do some more manipulation that the object won't know about. Is there a way to do this?

Currently:

lif.collection_select(:tier_id, @lif_plan.tiers, :id, :tiername) 

I tried this and it didn't work (thinking it would pass the tier object in)

lif.collection_select(:tier_id, @lif_plan.tiers, :id, {|tier| "#{tier.tiername} - #{@member.tierrate(year_of_rate)} "} ) 

My challenge is that the tier will not know what it should be doing without additional information so I can't just make a function in there to return. Any help or suggestions would be appreciated!


Solution

  • You were almost there, remember that collection_select is expecting a Proc/Method there. So it will try to send a :call message to it.

    Change it to the following and try again:

    lif.collection_select(:tier_id, @lif_plan.tiers, :id, Proc.new {|tier| "#{tier.tiername} - #{@member.tierrate(year_of_rate)} "} )