Recently I’ve resolved one more problem which concerns presence validator and belongs_to association. But it’s involved another thing - accepts_nested_attributes. Assume you have nested form and parent object is not exist yet, so you can have this issue. Here I will show you how to fix it.
Issue
Let’s we have the following models and their relations:
Go to rails console and check the validator out:
So this is an our challenge - we can’t save account, because user can’t pass validation rules because account (parent model) is not existed yet! But to remove validator won’t be right solution. Are you agree with me?
Solution
A solution for this issue is very simple - just add to has_one (or if you have has_many) one more option - :inverse_of. With this option rails won’t try to get account from database when user is validated. The account will be got from memory. If you don’t familiar with this option I strongly recommend you to read an official rails guide.
So, change account model try it:
I hope this article was helpful for you.