views:

92

answers:

2

I'm new to Rails and am working with the collection_select method.

I have two fields I'd like to display in my select box:

first_name and last_name

So far, I can only display one or the other, not both.

Here's the code I'm working with:

collection_select(:hour,:shopper_id,@shoppers,:id,"last_name")

Thank you.

+3  A: 

Add full_name method to shopper model:

class Shopper < ActiveRecord::Base
  #.....
  # add this
  def full_name
    "#{first_name} #{last_name}"
  end
end

And modify the collection_select statement:

collection_select(:hour,:shopper_id,@shoppers,:id,:full_name)

This is because most of Rails helpers takes methods names as params, so does collection_select, which takes a text_method param, which is the name of the method to be called to generate the text of the option itself, thus we define full_name method and we pass it's name to collection_select.

khelll
Great, thank you!
New2rails
A: 

Hello,

My problem is somewhat similar.

The difference is:

  1. here first name and last name belong to same model.

  2. In my case the two attributes which need to be joined are in different models.

Any help appreciated.

Thanks

aliceinwonderland