3

I'm trying to display active projects per party in a drop down list. active_projects is a method within the Party model. The grouped_collection_select code below works however, when I attempt to convert my form into a simple_form, my active_projects method is no longer recognised.

Below are my two code extracts. The first working correctly while the other causes an error.

# rails default
<%= f.grouped_collection_select(:project_id, 
                                 Party.all, 
                                 :"active_projects(#{date.strftime("%Y%m%d")})", 
                                 :party_name, 
                                 :id, :project_name) %>
# simple form
<%= f.input :project_id, 
            collection: Party.all, as: :grouped_select,
            group_method: :"active_projects(#{date})" %>

1 Answer 1

0

I know this one is a little old but I have a solution to this problem using simple_form. I am not sure if it is the best solution but it does work.

Basically, the issue comes down to passing in a value to the group_method. In my case I had a class that needed to get the current_users company that he/she belongs to. My model/database structure was like this:

Type -> Category

In my case the Type records were global and did not belong to a specific company. However, the category model records did belong to a specific company. The goal is to show a grouped select with global types and then company-specific categories underneath them. Here is what I did:

class Type < ActiveRecord::Base

  has_many :categories
  attr_accessor :company_id

  # Basically returns all the 'type' records but before doing so sets the
  # company_id attribute based on the value passed. This is possible because
  # simple_form uses the same instance of the parent class to call the            
  # group_by method on.

  def self.all_with_company(company_id)
    Type.all.each do |item|
      item.company_id = company_id
    end
  end

  # Then for my group_by method I added a where clause that reuses the
  # attribute set when I originally grabbed the records from the model.

  def categories_for_company
    self.categories.where(:company_id => self.company_id)
  end


end

So the above is a definition of the type class. For reference here is my definition of the category class.

class Category < ActiveRecord::Base

  belongs_to :company
  belongs_to :type

end

Then on my simple_form control I did this:

<%= f.association :category, :label => 'Category', :as => :grouped_select, :collection => Type.all_with_company(company_id), :group_method => :categories_for_company, :label_method => :name %>

Basically instead of passing in the value we want to filter on in the :group_method property we pass it in on the :collection property. Even though it will not be used to get the parent collection it is just being stored for later use in the class instance. This way, when we call another method on that class it has the value we need to do our filtering on the child.

2
  • f.association :category, as: :grouped_select, collection: Category.where(company_id: company_id).group_by(&:type), group_method: :last, group_label_method: :first is pretty crazy...
    – Alexis
    Apr 18, 2019 at 18:53
  • by the way, for the labels try to have defined a to_s method that it could use, but i believe that it will use name or to_s
    – Alexis
    Apr 18, 2019 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.