我正在尝试
select_tag "employee_compensation_benefits_selection", options_from_collection_for_select(@employees, "id", "entity.name", "1")
但是entity.name不会工作会抛出一个未定义的方法`entity.name'。 “entity”属于另一个模型。通过entity_id
class Employee < ActiveRecord::Base
include UUIDHelper
belongs_to :entity
has_one :status
has_many :restdays
has_one :regular_work_period
validates_presence_of :entity
end
require 'file_size_validator'
class Entity < ActiveRecord::Base
include UUIDHelper
has_one :access, autosave: true
has_one :employee, autosave: true
has_many :contact_detail, autosave: true
has_many :file_set
has_many :link_set
mount_uploader :logo, AvatarUploader
validates_presence_of :name
validates :name, uniqueness: true
validates_length_of :description, maximum: 256
validates :logo,
:file_size => {
:maximum => 25.megabytes.to_i
}
end
答案 0 :(得分:1)
您可以向员工添加一个可以调用的方法,例如:
class Employee < ActiveRecord::Base
...
def entity_name
self.entity.name
end
end
然后:
select_tag "employee_compensation_benefits_selection", options_from_collection_for_select(@employees, "id", "entity_name", "1")
或者您可以使用lambda而不是添加方法:
select_tag "employee_compensation_benefits_selection", options_from_collection_for_select(@employees, "id", lambda { |employee| employee.entity.name }, "1")
答案 1 :(得分:0)
您可以将其添加到您的员工模型中:
def entity_name
self.entity.name
end
或者您可以将这样的委托添加到您的Employee模型中:
delegate :name, :to => :entity, :prefix => true
然后你可以这样做:
select_tag "employee_compensation_benefits_selection", options_from_collection_for_select(@employees, "id", "entity_name", "1")
答案 2 :(得分:0)
&#39;实体&#39; model是指一个表名&#39; entity&#39;在数据库上(除非你覆盖了表名的默认值)。使用&#34; entities.name&#34;代替。如果您加入结果@employees = Employer.joins(:entity),这将有效。