如何查找具有当前用户的 Company_id
How to lookup Company_id that has the current User
我想用当前用户的 company_id
创建一个新的 jobdescription
,但它不起作用,我尝试了不同的方法,但它仍然不起作用。
所以,我不知道如何在我的控制器中构建查询,如果您能告诉我问题出在哪里,那将非常感谢。谢谢。
Schema.rb
ActiveRecord::Schema.define(version: 20150212182839) do
create_table "companies", force: true do |t|
t.string "company_name"
t.string "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "jobdescriptions", force: true do |t|
t.string "applyjobid"
t.string "job_title"
t.string "department"
t.text "shift"
t.string "work_location"
t.string "position_supervisor"
t.string "supervisor_position_supervisor"
t.decimal "rate_pay"
t.text "benefits"
t.text "other_benefits"
t.text "job_summary"
t.text "job_duties"
t.text "tasks"
t.text "results"
t.text "responsibilities"
t.text "knowledge"
t.text "skills"
t.text "abilities"
t.text "physical_requirement"
t.text "work_envir_condition"
t.text "protective_clothing_and_devices_required"
t.text "tools_or_equipment_required"
t.text "qualifications"
t.text "education_and_training"
t.text "license_certification"
t.text "experience"
t.text "aptitudes_interests_temperament"
t.text "roles_relationships"
t.text "supervisory_responsibility"
t.text "advancement_promotion_opportunities"
t.string "internal_comment"
t.string "submited_by"
t.string "approved_by"
t.string "statut"
t.date "from_date"
t.date "end_date"
t.integer "company_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "jobdescriptions", ["company_id"], name: "index_jobdescriptions_on_company_id"
create_table "users", force: true do |t|
t.string "username"
t.string "country"
t.string "role_id"
t.integer "company_id"
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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.integer "current_sign_in_ip"
t.integer "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["company_id"], name: "index_users_on_company_id"
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "users_roles", id: false, force: true do |t|
t.integer "user_id"
t.integer "role_id"
end
add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
end
这是我的JobdescriptionsController
class JobdescriptionsController < ApplicationController
layout 'application'
def new
@jobdescription = Jobdescription.new
end
def create
@jobdescription = current_user.company_id.jobdescriptions.create(jobdescription_params)
redirect_to new_jobdescription_path
end
private
def jobdescription_params
params.require(:jobdescription).permit(:applyjobid, :job_title, :work_location, :rate_pay, :shift, )
end
end
型号Jobdescription
class Jobdescription < ActiveRecord::Base
belongs_to :company
end
型号Company
class Company < ActiveRecord::Base
has_many :users
has_many :jobdescriptions
end
型号User
class User < ActiveRecord::Base
rolify
default_value_for :role_id, "1"
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :timesheets
has_many :expenses
belongs_to :company
end
我认为问题在于 current_user.company_id
给出了该用户公司的 company_id
的整数值,而您真正想要的是获取公司对象实例本身。然后您可以在该公司实例上调用 create
。
因此 create
方法中 @jobdescription
的赋值将是:
@jobdescription = current_user.company.jobdescriptions.create(jobdescription_params)
我想用当前用户的 company_id
创建一个新的 jobdescription
,但它不起作用,我尝试了不同的方法,但它仍然不起作用。
所以,我不知道如何在我的控制器中构建查询,如果您能告诉我问题出在哪里,那将非常感谢。谢谢。
Schema.rb
ActiveRecord::Schema.define(version: 20150212182839) do
create_table "companies", force: true do |t|
t.string "company_name"
t.string "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "jobdescriptions", force: true do |t|
t.string "applyjobid"
t.string "job_title"
t.string "department"
t.text "shift"
t.string "work_location"
t.string "position_supervisor"
t.string "supervisor_position_supervisor"
t.decimal "rate_pay"
t.text "benefits"
t.text "other_benefits"
t.text "job_summary"
t.text "job_duties"
t.text "tasks"
t.text "results"
t.text "responsibilities"
t.text "knowledge"
t.text "skills"
t.text "abilities"
t.text "physical_requirement"
t.text "work_envir_condition"
t.text "protective_clothing_and_devices_required"
t.text "tools_or_equipment_required"
t.text "qualifications"
t.text "education_and_training"
t.text "license_certification"
t.text "experience"
t.text "aptitudes_interests_temperament"
t.text "roles_relationships"
t.text "supervisory_responsibility"
t.text "advancement_promotion_opportunities"
t.string "internal_comment"
t.string "submited_by"
t.string "approved_by"
t.string "statut"
t.date "from_date"
t.date "end_date"
t.integer "company_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "jobdescriptions", ["company_id"], name: "index_jobdescriptions_on_company_id"
create_table "users", force: true do |t|
t.string "username"
t.string "country"
t.string "role_id"
t.integer "company_id"
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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.integer "current_sign_in_ip"
t.integer "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["company_id"], name: "index_users_on_company_id"
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "users_roles", id: false, force: true do |t|
t.integer "user_id"
t.integer "role_id"
end
add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
end
这是我的JobdescriptionsController
class JobdescriptionsController < ApplicationController
layout 'application'
def new
@jobdescription = Jobdescription.new
end
def create
@jobdescription = current_user.company_id.jobdescriptions.create(jobdescription_params)
redirect_to new_jobdescription_path
end
private
def jobdescription_params
params.require(:jobdescription).permit(:applyjobid, :job_title, :work_location, :rate_pay, :shift, )
end
end
型号Jobdescription
class Jobdescription < ActiveRecord::Base
belongs_to :company
end
型号Company
class Company < ActiveRecord::Base
has_many :users
has_many :jobdescriptions
end
型号User
class User < ActiveRecord::Base
rolify
default_value_for :role_id, "1"
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :timesheets
has_many :expenses
belongs_to :company
end
我认为问题在于 current_user.company_id
给出了该用户公司的 company_id
的整数值,而您真正想要的是获取公司对象实例本身。然后您可以在该公司实例上调用 create
。
因此 create
方法中 @jobdescription
的赋值将是:
@jobdescription = current_user.company.jobdescriptions.create(jobdescription_params)