Rails form search or create
<p>In a Product model where it as an associated brand, I would like to have an input field where I can select an existing brand or add a new one.</p>

<p>In the form I have:</p>

<pre><code>&lt;%= f.fields_for :brand do |b| %&gt;
&lt;div class="form-group"&gt;
  &lt;%= b.label :name, t('brand.one') %&gt;
  &lt;%= b.select :name, options_from_collection_for_select(Brand.all, :name, :name, product.brand.name), { include_blank: true}, class: '0select2-find-or-create' %&gt;
&lt;/div&gt;
&lt;% end %&gt;
</code></pre>

<p>and in the models:
Product:</p>

<pre><code>belongs_to :brand
accepts_nested_attributes_for :brand, limit: 1
</code></pre>

<p>Brand:</p>

<pre><code>has_many :products
</code></pre>

<p>But every time I change the Brand of a product it will change that brand's (by brand id) name, instead of changing the id.</p>

<p>Also for the part of creating a new brand I will try to use <a href="https://select2.github.io" rel="nofollow">select2</a> with the <code>tags</code> option. Any other suggestion?</p>