【发布时间】:2026-02-01 12:55:01
【问题描述】:
我的 Rails 应用程序中有一个下拉选择列表,用于创建新记录。
<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>
这样用户就可以从测量员的姓名列表中选择他们想要与他们正在创建的案例相关联的姓名。
我想做的是将测量员的公司名称列在他们的名字旁边的括号中。
目前代码如下:
<li>Appointed Company<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (@kase.appointedsurveyor_id rescue "")} %></span></li>
<li>Appointed Surveyor<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>
是否可以让测量员工作的公司以他们的名字输出?
关联如下:
class Company < ActiveRecord::Base
has_many :kases
has_many :people
class Person < ActiveRecord::Base
has_many :kases # foreign key in join table
belongs_to :company
我也将公司记录的输出改为他们的名字而不是他们的身份证号。
def to_s; companyname; end
提前致谢!
谢谢,
丹尼
【问题讨论】:
标签: ruby-on-rails activerecord select associations