jeudi 4 juin 2015

Rails using build with checkbox with has_one association

I have user that has_one profile but not all users need a profile. I'm looking for a way to create the profile only when a checkbox is checked on the user form (either via update or create).

My models look like this -

class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile

class Profile < ActiveRecord::Base    
belongs_to :user

Ideally in my user form, I'd like to include a checkbox, when checked this would create the profile and set user_id in profile to the corresponding user ID.

I know that in my Users Controller, doing

@user.build_profile

Will create the profile on update but again, not all users need a profile created.

Aucun commentaire:

Enregistrer un commentaire