{"record":{"id":"f29102824ee7fa21","repo":"heartcombo/simple_form","slug":"has-one-associations-are-not-supported-by-f-assoc","errorCode":null,"errorMessage":":has_one associations are not supported by f.association","messagePattern":":has_one associations are not supported by f\\.association","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/simple_form/form_builder.rb","lineNumber":523,"sourceCode":"        else\n          order = reflection.options[:order]\n          conditions = reflection.options[:conditions]\n          conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)\n\n          relation = relation.where(conditions) if relation.respond_to?(:where) && conditions.present?\n          relation = relation.order(order) if relation.respond_to?(:order)\n        end\n\n        relation\n      end\n    end\n\n    def build_association_attribute(reflection, association, options)\n      case reflection.macro\n      when :belongs_to\n        (reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :\"#{reflection.name}_id\"\n      when :has_one\n        raise ArgumentError, \":has_one associations are not supported by f.association\"\n      else\n        if options[:as] == :select || options[:as] == :grouped_select\n          html_options = options[:input_html] ||= {}\n          html_options[:multiple] = true unless html_options.key?(:multiple)\n        end\n\n        # Force the association to be preloaded for performance.\n        if options[:preload] != false && object.respond_to?(association)\n          target = object.send(association)\n          target.to_a if target.respond_to?(:to_a)\n        end\n\n        :\"#{reflection.name.to_s.singularize}_ids\"\n      end\n    end\n\n    # Find an input based on the attribute name.\n    def find_input(attribute_name, options = {}, &block)","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/heartcombo/simple_form/blob/18f38aad0bdeca2ba1815043b94d96fdbbe6a325/lib/simple_form/form_builder.rb#L505-L541","documentation":"When f.association resolves the reflection, build_association_attribute maps belongs_to to the foreign-key attribute and has_many/has_and_belongs_to_many to multi-select collections; a :has_one target has no foreign key on the parent and no sensible single select, so it raises ArgumentError instead of rendering something wrong (lib/simple_form/form_builder.rb:523). A singular associated record is meant to be edited with nested fields, not an association input.","triggerScenarios":"f.association :profile on a model that declares has_one :profile; f.association :account where the reflection macro is :has_one (including polymorphic has_one); code that enumerates all reflections and feeds each to f.association.","commonSituations":"Assuming f.association works for every association macro; trying to shortcut a nested profile/account form with one call; scaffolding or admin generators that list reflections automatically.","solutions":["Replace it with nested fields: f.simple_fields_for :profile do |pf| ... end, with accepts_nested_attributes_for :profile on the model","If you really need one selectable record, model the relation as belongs_to on this model (a *_id column) and use f.association on that","Otherwise render manual inputs for the associated object's attributes via simple_fields_for"],"exampleFix":"# before\n<%= f.association :profile %>  <!-- ArgumentError: :has_one not supported -->\n\n# after (model: has_one :profile + accepts_nested_attributes_for :profile)\n<%= f.simple_fields_for :profile do |pf| %>\n  <%= pf.input :bio %>\n<% end %>","handlingStrategy":"validation","validationCode":"reflection = f.object.class.reflect_on_association(:profile)\nif reflection&.macro == :has_one\n  f.simple_fields_for :profile do |pf|\n    render 'profiles/fields', f: pf\n  end\nelse\n  f.association :profile\nend","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use f.association only for belongs_to, has_many and has_and_belongs_to_many reflections","Pair has_one records with accepts_nested_attributes_for and edit them via f.simple_fields_for","If a shared partial iterates reflections, filter out macro :has_one before calling association"],"tags":["simple-form","association","has-one","nested-attributes","argument-error"],"backgroundTag":"unsupported-association-macro","analyzedSha":"18f38aad0bdeca2ba1815043b94d96fdbbe6a325","analyzedAt":"2026-08-21T18:23:08.988Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}