I have a “grouped_collection_select” for a “branch” field, in combination with a “sector” field. Sector has_many branches and branch belongs_to sector:
<%= f.grouped_collection_select(:branch, current_user.company.sectors.order(:sector), :branches, :sector, :id, :branch, include_blank: true) %>
This works, but the “:branches” shows all branches and should only show the branches of the current_user.company, just like the sectord. But when I change “:branches” into “current_user.company.branches.order(:branch)”, I get an error:
(eval):1: syntax error, unexpected $end group.#<ActiveRecord
What am I doing wrong here? Thanks!
For clarity (and semantics) set that collection in your controller to use in your view:
…or if you want to limit the branches that are showing for each sector by user, you’d probably want to build the collection based on the user’s branches. One example:
Then assuming you want to set the branch_id AND a sector has a name attribute AND the branch has a name attribute, this should work: