简单表单集合,但不包括特定记录

时间:2014-01-31 16:56:26

标签: ruby-on-rails simple-form

以下是我用来生成公司选择列表的代码。

<%= f.association :company, :collection => Company.order('name ASC'), :label => "Company: ", :include_blank => false %>

现在该表预先填充了特殊记录“id:1,name:none”,我想将此记录排除为生成的选择列表中的可选选项。我怎样才能完成这个?

谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个

<%= f.association :company, :collection => Company.where("id != 1").order('name ASC'), :label => "Company: ", :include_blank => false %>

或者

In the controller

   @companies = Company.where("id != 1").order('name ASC')

In the view

    <%= f.association :company, :collection => @companies, :label => "Company: ", :include_blank => false %>