Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中建立员工-公司关系?_Ruby On Rails_Activerecord_Belongs To - Fatal编程技术网

Ruby on rails 如何在Rails中建立员工-公司关系?

Ruby on rails 如何在Rails中建立员工-公司关系?,ruby-on-rails,activerecord,belongs-to,Ruby On Rails,Activerecord,Belongs To,我正在尝试在RubyonRails上创建一个工作板。我正在使用Desive处理帐户,我有一个员工(设备用户)和公司模型 在这个挑战中,当有人以员工身份创建帐户时,我必须检查公司域是否已经存在(该域从员工电子邮件中提取),如果不存在,则必须将员工重定向到用于注册公司的页面。此外,第一位员工成为公司的管理人员 问题是,如何处理好两者之间的关系呢?如果我用一个属于彼此的关系来创造一个没有公司的员工是不可能的,反之亦然。如果我不这样做,以后就无法访问一些ID来建立正确的关系。更糟糕的是,稍后我需要为招聘

我正在尝试在RubyonRails上创建一个工作板。我正在使用Desive处理帐户,我有一个员工(设备用户)和公司模型

在这个挑战中,当有人以员工身份创建帐户时,我必须检查公司域是否已经存在(该域从员工电子邮件中提取),如果不存在,则必须将员工重定向到用于注册公司的页面。此外,第一位员工成为公司的管理人员

问题是,如何处理好两者之间的关系呢?如果我用一个属于彼此的关系来创造一个没有公司的员工是不可能的,反之亦然。如果我不这样做,以后就无法访问一些ID来建立正确的关系。更糟糕的是,稍后我需要为招聘广告创建一个招聘广告模型,它应该同时属于公司和员工。那么,有没有办法在没有“员工没有公司,反之亦然”问题的情况下实现这一点

这是我的schema.rb

ActiveRecord::Schema.define(version: 2021_02_23_032327) do

  create_table "applicants", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_applicants_on_email", unique: true
    t.index ["reset_password_token"], name: "index_applicants_on_reset_password_token", unique: true
  end

  create_table "companies", force: :cascade do |t|
    t.string "name"
    t.string "logo"
    t.string "address"
    t.string "cnpj"
    t.string "site"
    t.string "social_media"
    t.string "domain"
    t.integer "employee_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["employee_id"], name: "index_companies_on_employee_id"
  end

  create_table "companies_employees", id: false, force: :cascade do |t|
    t.integer "employee_id", null: false
    t.integer "company_id", null: false
    t.index ["company_id", "employee_id"], name: "index_companies_employees_on_company_id_and_employee_id"
    t.index ["employee_id", "company_id"], name: "index_companies_employees_on_employee_id_and_company_id"
  end

  create_table "employees", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_employees_on_email", unique: true
    t.index ["reset_password_token"], name: "index_employees_on_reset_password_token", unique: true
  end

  create_table "job_ads", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.integer "salary_range"
    t.integer "level"
    t.date "mandatory_requirements"
    t.integer "total_vacancy"
    t.integer "company_id", null: false
    t.integer "employee_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["company_id"], name: "index_job_ads_on_company_id"
    t.index ["employee_id"], name: "index_job_ads_on_employee_id"
  end

  add_foreign_key "companies", "employees"
  add_foreign_key "job_ads", "companies"
  add_foreign_key "job_ads", "employees"
end
我现在正在尝试的是:

员工的设备注册_controller.rb

class Employees::RegistrationsController < Devise::RegistrationsController
 
  def after_sign_up_path_for(resource)
    domain = resource.email.split('@').last
    if domain_is_free?(domain)
      @employee.save
      redirect_to new_company_path
      flash[:notice] = "Não identificamos o domínio #{domain}, por favor registre-o"
    else
      redirect_to root_path
    end
  end

  def create
    @employee = Employee.new(employee_params)
    if @employee.save
      sign_in(@employee)
      after_sign_up_path_for(@employee)
    else
      render :new
    end
  end

  protected

  def domain_is_free?(employee_mail)
    !Company.exists?(['domain LIKE ?', "%#{employee_mail.split('@').last}%"])
  end 

  def employee_params
    params.require(:employee)
        .permit(:email, :password, :password_confirmation)
  end
最后是错误消息

Failure/Error:
   params.require(:company)
         .permit(:name, :logo, :address, :cnpj, :site, :social_media, :domain).merge(employee_id: current_employee.id)
 
 ActionController::ParameterMissing:
   param is missing or the value is empty: company
   Did you mean?  controller
                  action

我认为您对此处的关联感到困惑,请尝试从
company.rb
文件中删除
属于:employee

因此,“属于”的意思是模型A的一个实例属于模型B的一个实例。这意味着没有B的实例A的实例就不可能存在(例如,书没有作者就不可能存在)。查看此以了解更多信息


另外,由于您希望员工成为公司的管理员,请在employee.rb中将admin字段添加为布尔值。

我不确定我是否了解所有内容


  • 定义
    所属关系时,必须为此表提供外键。在
    employee.rb
    中,您编写
    ruby所属:company,可选:true
    ,但不要将
    company\u id
    字段添加到
    employees
    表中。谢谢你的回复!我在employee.rb中有一个company_id,但我删除了它,因为我试图让一个company_id为空的员工稍后添加它,但它似乎太复杂了。我将尝试您的建议,但是否可以使用嵌套属性在同一控制器中创建员工和公司?
    class Employee < ApplicationRecord
      
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :validatable
      belongs_to :company, optional: true                  
            
    end
    
        require 'rails_helper'
    
    feature 'employee creates an account' do
      scenario 'and is the first to register a domain' do
        visit root_path
        click_on 'Employee registration'
    
        within('form') do
          fill_in 'Email', with: 'roger@jobs.com'
          fill_in 'Password', with: '123456'
          fill_in 'Password confirmation', with: '123456'
          click_on 'Sign up'
        end
    
        expect(current_path).to eq new_company_path
        expect(page).to have_content 'There is no company named jobs, please register it'  
        expect(page).to have_content 'Register Company'
      end
    
    Failure/Error:
       params.require(:company)
             .permit(:name, :logo, :address, :cnpj, :site, :social_media, :domain).merge(employee_id: current_employee.id)
     
     ActionController::ParameterMissing:
       param is missing or the value is empty: company
       Did you mean?  controller
                      action