简体   繁体   中英

Rails display attribute from a has_and_belongs_to_many relationship

So I have Employees and Regions. Employees may/may not belong to a Region just like Region may/may not have an Employee.

Models:

Employee
 has_and_belongs_to_many :region, optional: true
Region
 has_and_belongs_to_many :employees, optional: true

I have a join table that looks like:

create_table "employees_regions", id: false, force: :cascade do |t|
 t.integer "employee_id"
 t.integer "region_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.index ["employee_id"], name: "index_employees_regions_on_employee_id"
 t.index ["region_id"], name: "index_employees_regions_on_region_id"
end

My Employee Controller:

class Admin::EmployeesController < Admin::ApplicationController
  belongs_to_app :employees
  add_breadcrumb 'Employees', :admin_employees_path
  before_action :load_employee, only: [:show, :edit, :update, :destroy]

  def index
    @employees = Employee.ordered.paginate(page: params[:page])
    @employees = @employees.search(params[:search]) if params[:search]
    respond_with @employees
  end

  def show
    respond_with @employee
  end

  def new
    @employee = Employee.new
    respond_with @employee
  end

  def create
    @employee = Employee.new(employee_params)
    flash[:notice] = 'Employee created successfully' if @employee.save
    respond_with @employee, location: admin_employees_path
  end

  def edit
    respond_with @employee
  end

  def update
    flash[:notice] = 'Employee updated successfully' if @employee.update_attributes(employee_params)
    respond_with @employee, location: admin_employees_path
  end

  def destroy
    flash[:notice] = 'Employee deleted successfully' if @employee.destroy
    respond_with @employee, location: admin_employees_path
  end

  private

  def load_employee
    @employee = Employee.find_by!(id: params[:id])
  end

  def employee_params
    params.require(:employee).permit(:first_name, :last_name, :title, :leadership, :bio, :avatar, :region, :region_id)
  end
end

Then in my view:

.table-responsive
table.table.table-striped.table-hover
  thead
    tr
      th First Name
      th Last Name
      th Title
      th Avatar
      th Leadership
      th Region
      th &nbsp;
  tbody
    - @employees.each do |employee|
      tr
        td = employee.first_name
        td = employee.last_name
        td = employee.title
        td = image_tag employee.avatar
        td = employee.leadership
        td = employee.region.map(&:name).join(', ')

And here comes the issue. For the Region it displays nothing. I've tried the following: employee.region_id (shows the correct id associated), employee.region(&:name) (shows Region::ActiveRecord_Associations_CollectionProxy123), employee.region.name (shows Region), and employee.region&.name (shows Region).

What I'm looking to see is the region name. So if an employee works in Northeast it should say Northeast not Region. How do I hit the region's name on the employee view?

Updated the model to use plural then updated the controller's params to:

 def employee_params
  params.require(:employee).permit(:first_name, :last_name, :title, :leadership, :bio, :avatar, :regions).tap do |employee|
  employee[:region_ids] = [employee[:regions].to_i]
  employee.delete(:regions)
 end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM