1

I am trying to get a dropdown list of projects organized according to the owner name. For doing this I am using SimpleForm and the grouped_select method. The issue is that for now it is a hash that is send back to the dropdown list and not the name of the project. How can I get solely the name of the project within this hash ?

This is how I define my collection

 def option_clients_projects
        unless self.contractor.nil?
          opt_clients = self.contractor.construction_projects.map{ |cp| {:construction_project => cp.name, :client_name => cp.client.name} }
          opt_clients << {:construction_project =>"Add a new project", :client_name  => ""}
          opt_clients
        end
      end

//This is what i get out this method//

[{:construction_project=>"Toilette fr", :client_name=>"Martine depouhon"}, {:construction_project=>"démolition Chateau", :client_name=>"Carla"}]

>

This is my input as grouped_select within my SimpleForm

    <%= f.input :construction_project_id, collection: current_user.option_clients_projects.group_by{ |d| d[:client_name] }, as: :grouped_select, group_method: :last, group_label_method: :first %>

//The result of the group_by operation//

 {"Martine depouhon"=>[{:construction_project=>"Toilette fr", :client_name=>"Martine depouhon"}], "Carla "=>[{:construction_project=>"démolition Chateau 2 2", :client_name=>"Carla "}]}
    >

The visual result of my code

group_method - The name of a method which, when called on a member of collection, returns an array of child objects representing the tags.

Raoul Gthr
  • 53
  • 6
  • I think I need to create personalised method to act on my collection in order to replace "group_method: :last" which is not doing what I want obviously. Could be a possibility : https://stackoverflow.com/questions/4845107/creating-custom-method-for-collection – Raoul Gthr Jan 11 '19 at 15:43
  • The problem is that I don't know where and how to create this new method that is supposed to replace "group_method: :last". What am i supposed to return with this new method? – Raoul Gthr Jan 14 '19 at 08:44

0 Answers0