Skip to content

Instantly share code, notes, and snippets.

@walshyb
Created August 14, 2020 17:46
Show Gist options
  • Save walshyb/a71379aa5e325ec1e3cc1e5877538261 to your computer and use it in GitHub Desktop.
Save walshyb/a71379aa5e325ec1e3cc1e5877538261 to your computer and use it in GitHub Desktop.
Updated user model that belongs to Contact, who has the EmailAddress association
# app/models/user.rb
class User
belongs_to :contact
# This is optional
has_many :email_addresses, through: :contact
# So we can create the contact association when creating a User
accepts_nested_attributes_for :contact
after_initialize :add_contact
# Create new blank contact on new record initialize.
# This makes it so a blank Contact is attached when when
# we create a new User
def add_contact
self.contact ||= Contact.new if self.new_record?
end
# ...
# Then below are the same devise overrides from the
# user.rb shown above. There are no changes to them.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment