Best way to instanciate an empty address element in many nested fields_for


I have 3 models, User, Company and Address.

Basically, a User belongs to a Company and an Address has one Company (because it can be linked to other models).

In a form I want to display a User, its Company and its company Address, with fields_for.

The problem is when my User has a Company, that does not have an Address: the part containing the Address is not displayed.

I see 2 ways of instanciating my empty adress:

  1. In the view:

f.fields_for :company, do |company_form|

and company_form.fields_for :address, (address = ? address :

  1. In the controller

@ = unless @

(a variant would be to write, which I prefer over

Do you see other ways and what do you think is best?


I would use

@company.build_address unless @company.address

Garrett Lancaster