views:

122

answers:

1

In my application i have a collection_select on my members table, the members table only contains id's. It has an user_id, project_id, role_id, and so on.

I want show the members name in the collection_select. But i only have the user_id in the members table, how can i show the names from the user table as options?

<%= collection_select(nil, :member_id, members, :id, :user_id,
                 {:prompt   => "Select a member"}) %>

Now the select box shows the options 1,2,3,4, and so on. it needs to be name1, name2, from the user table.

Does any one has experiance with this?

+1  A: 

First of all you need to add a new method to your members model:

class Member < ActiveRecord::Base
  belongs_to :user
  def member_name
    user.name
  end
end

Then change the text_method argument to member_name:

<%= collection_select(:member, :member_id, members, :id, :member_name,
                 {:prompt   => "Select a member"}) %>
khelll