- 5 months ago 29
Ruby Question

collection_select show multiple object attributes?

In addition to showing the challenge's

. I also want to show its
next to the

It would look like this for example:

Visit London 09/09/16
Make $1,000,000 10/15/18
Knit a Scarf 01/11/19

Instead of just this:

enter image description here

<%= f.collection_select :challenge_id, current_user.challenges.order(:deadline),:id,:name, include_blank: true %>


Define a method name_with_deadline in challenge.rb

def name_with_deadline
  "#{name} #{deadline}"

and then make use of this method as label in the collection.

<%= f.collection_select :challenge_id, current_user.challenges.order(:deadline),:id, :name_with_deadline, include_blank: true %>

The name_with_deadline method will called for every object in the collection to retrieve the label text.

Hope this helps!