0

我有一个由以下模型组成的表格

员工.rb

class Employee < ActiveRecord::Base

      attr_accessible :employee_number, :joining_date, :first_name, :middle_name, :last_name,
      :gender, :job_title, :employee_department_id, :qualification, :experience_detail,
      :experience_year, :experience_month, :status_description, :date_of_birth, :marital_status,
      :children_count, :father_name, :mother_name, :husband_name, :blood_group, :nationality_id,
      :home_address_line1, :home_address_line2, :home_city, :home_state, :home_pin_code,
      :office_address_line1, :office_address_line2, :office_city, :office_state, :office_pin_code,
      :office_phone1, :office_phone2, :mobile_phone, :home_phone, :email, :fax, :user_id,
      :reporting_manager_id, :employee_grade_id, :office_country_id,
      :home_country_id, :employee_category, :employee_position_id

      belongs_to :employee_department

      has_many :counselor_supervisors
      belongs_to :employee_position

      def to_label
       full_name = first_name + " " + last_name
      end

    end

员工部门.rb

class EmployeeDepartment < ActiveRecord::Base
      attr_accessible :code, :name

      has_many :employees

      has_many :employee_positions

      has_many :counselor_supervisors

      has_many :batch_leadership_supervisors

    def to_label
      name
    end


    end

辅导员主管.rb

class CounselorSupervisor < ActiveRecord::Base
      attr_accessible :employee_id, :employee_department_id, :employee_position_id
      belongs_to :employee
      belongs_to :employee_department
      has_many :batch_counselor_supervisors

    def to_label
     employee.to_label
    end

    end

BatchCounselorSupervisor.rb

class BatchCounselorSupervisor < ActiveRecord::Base
   attr_accessible :counselor_supervisor_id , :employee_department_id , :counselor_batch_id, 
    :batch_counselor_advisors_attributes
  has_many :batch_counselor_advisors
  belongs_to :counselor_supervisor
  belongs_to :employee_department
  belongs_to :counselor_batch

  accepts_nested_attributes_for :batch_counselor_advisors

end

Employee_position.rb

class EmployeePosition < ActiveRecord::Base
  attr_accessible :position_title, :employee_department_id

  has_many :employees
  belongs_to :employee_department

  def to_label
    position_title
  end
end

batch_counselor_supervisors/new.html.erb (与我的问题相关的表格的一部分)

              <%= simple_form_for(@batch_counselor_supervisor) do |f| %>
              <%= f.error_messages %>
            <%= f.association :employee_department, as: :select %>
              <%= f.input :counselor_supervisor_id , collection: EmployeeDepartment.all, as: :grouped_select, group_method: :counselor_supervisors %>
<% end %>

下拉列表如下所示:

在此处输入图像描述

如果我添加了属于第一个部门的员工,"Business Administration"表单将正确显示如下:

在此处输入图像描述

更新:添加后label_method: :to_label,我的表格变成了这样:

 <%= simple_form_for(@batch_counselor_supervisor) do |f| %>
              <%= f.error_messages %>
            <%= f.association :employee_department, as: :select %>
              <%= f.input :counselor_supervisor_id ,
               collection: EmployeeDepartment.all, as: :grouped_select, group_method: :counselor_supervisors, label_method: :to_label %>
    <% end %>

员工姓名显示正确,但部门名称仍无法正确显示,如下图所示:

在此处输入图像描述

是这个SQLite3问题吗?如果是 sqlite3 问题,我该怎么做才能解决这个问题。

4

2 回答 2

0

据我所知,您只有标签未正确显示的问题。您能否尝试在输入中显式设置方法:

label_method: :to_label

有关更多信息,请查看https://github.com/plataformatec/simple_form并搜索 *label_method*

于 2013-11-09T10:17:33.583 回答
0

Employee表和EmployeePosition表中实际相关的行是什么?

在您的Employee表中,您碰巧有这两列; 员工 职位 ID 员工职位

由于employee_position也是一个表名,它是坏的/冗余的模型结构,可能会混淆您的查询的include 方法。有可能在您的表中,行已完全填写以完成第一个查询,但对于您的任何其他查询都没有,即使您认为是这样。

于 2013-11-08T20:18:22.890 回答