Question

I'm having some trouble with group_collection_select in one of my forms.

This is what the error I'm getting says:

undefined method `assert_valid_keys' for :company:Symbol

I've been troubleshooting for a while, but I just can't get this one.

My grouped_collection_code looks like this:

<%= f.grouped_collection_select :subsector_id, Sector.all, :subsectors, :sector_name, :id, :subsector_name %>

My models look like this:

class Sector < ActiveRecord::Base
  attr_accessible :sector_name
  has_many :companies
  has_many :subsectors
end

class Subsector < ActiveRecord::Base
  attr_accessible :sector_id, :subsector_name, :subsector_id
  belongs_to :sector, :company
end

class Company < ActiveRecord::Base
  belongs_to :sector
  has_many :subsectors, through: :sectors
end

I don't know if this is helpful, but the javascript that I have for the form looks like this:

jQuery ->
  subsectors = $('#company_subsector_id').html()
  $('#company_sector_id').change ->
    sector = $('#company_sector_id :selected').text()
    options = $(subsectors).filter("optgroup[label='#{sector}']").html()
    if options
      $('#company_subsector_id').html(options)
      $('#company_subsector_id').parent().show()
    else
      $('#company_subsector_id').empty()
      $('#company_subsector_id').parent().hide()

Can you help or provide direction as to how I can fix this error?

Was it helpful?

Solution

Your belongs_to declaration is causing this problem. You cannot have multiple names in your belongs_to declaration. Each association needs to be defined separately. Please change that to:

# Class Subsector < ActiveRecord::Base

belongs_to :sector
belongs_to :company

Have a look at the documentation for belongs_to here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top