Home > database >  Rails concatenation in each loop
Rails concatenation in each loop

Time:09-26

I would like to use concatenation in rails loop :

I tried :

collection: "Service.active.#{service.type}"

collection: Service.active. "#{service.type}"

<% services.each do |service| %>
   <div >
      <%= f.association :services, as: :check_boxes, collection: "Service.active.#{service.type}", input_html: { multiple: false } %>
   </div>
<% end %>

CodePudding user response:

If what you want is to dynamically invoke a method on Service you can use Object#public_send. If the method isn't public you can use send;

Service.active.public_send(service.type)
  • Related