Я совершенно новичок в Ruby и мне было интересно, как мне поступить, если я хочу, чтобы каждый раз, когда я создаю нового пользователя (при регистрации), он автоматически создавал экземпляр Исполнителя, так что новый Пользователь = Исполнитель.
Я совершенно заблудился, и было бы очень полезно получить помощь и объяснения.
Пользовательский контроллер
each_pair { |name, val| }class UsersController < ApplicationController
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :update_resource_params, if: :devise_controller?
def new
@user = User.new
end
def create
@user = User.new(user.params)
@contractor = Contractor.create(user: @user)
if @user.save
UserMailer.user_alert(@user).deliver_now
redirect_to @user, notice: 'User was successfully created.'
else
render :new
end
end
Модель подрядчика
class Contractor < ApplicationRecord
belongs_to :user
has_many :construction_projects, dependent: :destroy
has_many :clients, dependent: :destroy
has_many :documents, through: :construction_projects
has_many :resources
mount_uploader :logo, LogoUploader
# Model validations
validates :user, presence: true
validates_associated :construction_projects
validates_associated :clients
validates_associated :documents
validates_associated :resources
Модель пользователя
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :contractor
has_many :clients, through: :contractor
has_many :construction_projects, through: :contractor
# Model validations
validates_associated :clients
validates_associated :construction_projects
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
def option_projects
projects = self.contractor.construction_projects.map{ |cp| [cp.name, cp.id] }
projects << ["Add a new project", "Add a new project"]
projects
end
end