R
R
rusrich2018-09-12 19:56:44
Ruby on Rails
rusrich, 2018-09-12 19:56:44

How to make fields associated with a belongs_to association optional?

Good evening
. I have several columns in the model associated with the belongs_to association, in which I need to insert the id of another object.

class FirstModel < ApplicationRecord
belongs_to :secondmodel_object_a
belongs_to :secondmodel_object_b
belongs_to :secondmodel_object_c

The form is
<p>@firstmodel.title</p>
<p>@firstmodel.secondmodel_object_a</p>
<p>@firstmodel.secondmodel_object_b</p>
<p>@firstmodel.secondmodel_object_c</p>

First I want to save with title and secondmodel_object_a fields filled in. But the transaction is rolled back because secondmodel_object_b and secondmodel_object_c are not filled.
Tell me how this issue is resolved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oh_shi, 2018-09-12
@rusrich

belongs_to :secondmodel_object_b, optional: true
belongs_to :secondmodel_object_c, optional: true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question