Created
August 14, 2020 17:46
-
-
Save walshyb/a71379aa5e325ec1e3cc1e5877538261 to your computer and use it in GitHub Desktop.
Updated user model that belongs to Contact, who has the EmailAddress association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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