One way to achieve the desired result is that you can create a form that uses support for nested attributes in Rails:
<%= form_for(@user) do |f| %> <%= f.label :my_user_attribute %> <%= f.text_field :my_user_attribute %> <%= f.fields_for :profile do |fp| %> <p> <%= fp.label :my_profile_attribute %> <%= fp.text_field :my_profile_attribute %> </p> <% end %> <%= f.submit %> <% end %>
You also need to add the following to your User class:
accepts_nested_attributes_for :profile
You can learn more about active nested entry attributes here . You can read more about the helpers of the ActionView form here (search on the pages "Examples of nested attributes").
If you use this approach along with good validation on both models, you donβt have to worry about tracking database identifiers, because both of them will be created using ActiveRecord at the same time (but not until both model objects are valid).
source share