Geo
Geo

Reputation: 96947

Is it possible to conditionally allow nested attributes?

I have one big form, which ends up creating a User account, and completing part of it's profile. So, User belongs_to Profile. The thing is, I'd like to set the profile to be filled as nested attributes, but only for this form. All other forms for the User model should not allow sending nested attributes for the profile. Is this possible? How?

Upvotes: 1

Views: 835

Answers (1)

Gabe Kopley
Gabe Kopley

Reputation: 16687

You can easily conditionally reject nested attributes based on the contents of the attributes by passing a :reject_if block to accepts_nested_attributes_for.

For what you're trying to achieve, I'd simply not accept nested attributes, and instead call update_attributes on both the user and the profile in the controller action that is the target of the new user form.

Upvotes: 3

Related Questions