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
class RegistrationsController < Devise::RegistrationsController | |
protected | |
def sign_up_params | |
params.require(:user).permit( | |
:password, | |
:password_confirmation, | |
contact: [ | |
:name, | |
email_addresses_attributes: [ :email ] |
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/views/devise/registrations/new.html.erb | |
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> | |
<%= render "devise/shared/error_messages", resource: resource %> | |
<%= f.fields_for :contact do |c| %> | |
<div class="field"> | |
<%= c.label :name %><br /> | |
<%= c.text_field :name %> | |
</div> |
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 |
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/contact.rb | |
class Contact | |
has_one :user | |
has_many :email_addresses | |
accepts_nested_attributes_for :email_addresses | |
end |
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 | |
def email | |
email_address = email_addresses.where(primary: true).first | |
return email_address.email if email_address | |
end |
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
where(conditions.to_h) | |
.includes(:email_addresses) | |
.where(email_addresses: { | |
email: email, | |
primary: true | |
}).first |
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
# db/migrate/20200804153022_add_primary_to_email_addresses | |
class AddPrimaryToEmailAddresses < ActiveRecord::Migration[6.0] | |
def change | |
add_column :email_addresses, :primary, :boolean, default: false | |
add_index :email_addresses, :primary | |
end | |
end |
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/controllers/registrations_controller.rb | |
class RegistrationsController < Devise::RegistrationsController | |
protected | |
def sign_up_params | |
params.require(:user).permit( | |
:name, | |
:password, | |
:password_confirmation, | |
email_addresses: [ |
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/views/devise/registrations/new.html.erb | |
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> | |
<%= render "devise/shared/error_messages", resource: resource %> | |
<%= f.fields_for 'email_addresses_attributes[1]' do |e| %> | |
<div class="field"> | |
<%= e.label :email %><br /> | |
<%= e.email_field :email, autofocus: true, autocomplete: "email" %> | |
</div> | |
<% end %> |
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 | |
has_many :email_addresses | |
accepts_nested_attributes_for :email_addresses | |
# ... association definitions and other methods | |
# Define our overrides: |
NewerOlder