1

2 つの異なるモデルから 2 つの属性を表示するコレクションを選択しようとしています。

アカウントを選択したい。アカウントには名前と所有者がいます。所有者は属性 name を持つモデルです。コレクションを使用する場合は、[表示したい] を選択します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>

例: アカウントの名前はMain accountで、アカウントの所有者はStanです。選択すると、Stan-Main accountと表示されます。

一緒に働いた:

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

1 に答える 1

1

次のコードを試してください

 <%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %>
于 2016-03-02T12:13:47.837 に答える