2016-03-02 46 views
0

我正在尝试使集合选择显示来自两个不同模型的两个属性。Rails 5 collection_select:在一列中显示多个属性

我想选择一个帐户。该帐户有一个名称和一个所有者。所有者是一个也有属性名称的模型。 使用集合时,我希望它显示:account.name + owner.name。这是目前collection_select我有只显示account.name

<div class="field"> 
    <%= f.label :to_account_id %> 
    <%= f.collection_select :to_account_id, Account.all, :id, :name %> 
    </div> 

实例:帐户名主帐户和帐户的所有者是斯坦,选择它应该显示斯坦时 - 主帐户

曾与:

<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.owner.name} - #{a.name}", a.id] },:second,:first %> 

回答

0

尝试下面的代码

<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %> 
+0

谢谢,它的工作!只需要编辑代码abit :) – Hak