{"record":{"id":"7c4c0fd55acfc4ef","repo":"heartcombo/simple_form","slug":"association-cannot-be-used-in-forms-not-associated","errorCode":null,"errorMessage":"Association cannot be used in forms not associated with an object","messagePattern":"Association cannot be used in forms not associated with an object","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/simple_form/form_builder.rb","lineNumber":213,"sourceCode":"    # When a block is given, association simple behaves as a proxy to\n    # simple_fields_for:\n    #\n    #   f.association :company do |c|\n    #     c.input :name\n    #     c.input :type\n    #   end\n    #\n    # From the options above, only :collection can also be supplied.\n    #\n    # Please note that the association helper is currently only tested with Active Record. Depending on the ORM you are using your mileage may vary.\n    #\n    def association(association, options = {}, &block)\n      options = options.dup\n\n      return simple_fields_for(*[association,\n        options.delete(:collection), options].compact, &block) if block_given?\n\n      raise ArgumentError, \"Association cannot be used in forms not associated with an object\" unless @object\n\n      reflection = find_association_reflection(association)\n      raise \"Association #{association.inspect} not found\" unless reflection\n\n      options[:as] ||= :select\n      options[:collection] ||= fetch_association_collection(reflection, options)\n\n      attribute = build_association_attribute(reflection, association, options)\n\n      input(attribute, options.merge(reflection: reflection))\n    end\n\n    # Creates a button:\n    #\n    #   form_for @user do |f|\n    #     f.button :submit\n    #   end\n    #","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/heartcombo/simple_form/blob/18f38aad0bdeca2ba1815043b94d96fdbbe6a325/lib/simple_form/form_builder.rb#L195-L231","documentation":"f.association needs the form's @object to read the ActiveRecord reflection, infer the attribute name (e.g. category_id) and fetch the collection. If the builder has no object — as with simple_form_for :search — and no block is given, it raises ArgumentError (lib/simple_form/form_builder.rb:213). With a block it delegates to simple_fields_for instead and never raises.","triggerScenarios":"simple_form_for :search, method: :get do |f| f.association :category end (symbol form, object is nil); simple_form_for(@search) where @search is nil because the controller never assigned it; any builder whose object is nil calling association without a block.","commonSituations":"Search/filter forms that only need a dropdown of records; shared form partials reused for both model-backed and object-less forms; controller refactor that stops setting the instance variable the view builds its form from.","solutions":["For object-less forms, replace association with an explicit collection input: f.input :category_id, collection: Category.order(:name)","Back the form with a real object — a persisted or unsaved record, form object or presenter — so the reflection can be read","If you want nested fields, pass a block: f.association :category do |c| ... end, which delegates to simple_fields_for and bypasses the check"],"exampleFix":"# before\n<%= simple_form_for :search, method: :get do |f| %>\n  <%= f.association :category %>  <!-- ArgumentError: not associated with an object -->\n<% end %>\n\n# after\n<%= simple_form_for :search, method: :get do |f| %>\n  <%= f.input :category_id, collection: Category.order(:name), label_method: :name, value_method: :id %>\n<% end %>","handlingStrategy":"validation","validationCode":"if f.object.present?\n  f.association :category\nelse\n  f.input :category_id, collection: Category.order(:name)\nend","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reserve f.association for forms bound to a record instance","For search/filter forms, use f.input with an explicit :collection","In shared partials, branch on f.object.present? so both object-backed and object-less forms render","Set controller instance variables explicitly so simple_form_for @record never receives nil unexpectedly"],"tags":["simple-form","association","form-builder","nil-object","argument-error"],"backgroundTag":"missing-form-object","analyzedSha":"18f38aad0bdeca2ba1815043b94d96fdbbe6a325","analyzedAt":"2026-08-21T18:23:08.988Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}