Rails collection_select和Foreign_key

时间:2014-06-16 07:10:18

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 grouped-collection-select

以下是我的代码,

<%= f.collection_select :event_member_id, EventMember.all, :id, :company_member_id, :prompt => "Please select" %>

此处:company_member_idEventMember类中的foreign_key,我想将text_method显示为company_member_email而不是company_member_id,

所以我的collection_select应该如下所示,

<%= f.collection_select :event_member_id, EventMember.all, :id, :'company_member.email', :prompt => "Please select" %>

如何实现上述...... !!!

1 个答案:

答案 0 :(得分:1)

EventMember模态中: -

def company_member_email
  return self.company_member.email unless self.company_member.nil?
  ""
end

收藏品如下: -

<%= f.collection_select :event_member_id, EventMember.all, :id, :company_member_email, :prompt => "Please select" %>