Select and collect with conditions in Ruby on Rails

Posted on 12 April 2006

I really needed an option collection for a select box with confitions. In my case it needed to list vacancies people could apply for, but only if the vacancy was currently open. i.e. Check if it published or not?

I achieved it via this:

<%= f.select :vacancy_id, Vacancy.find(:all, :order => 'title', :conditions => ["publish = ?", 1]).collect { |v| [ v.title, v.id ] } %>

I even threw in an :order to sort the output by the vacancy title.

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • Can't see the code ;( Need it too!

    Chip at 21 Jul 09 at 13:47

  • it's blank

    seth at 28 Nov 09 at 18:41

  • Awesome, I was looking exactly for this.

    shishir at 11 Nov 10 at 23:25

Got something to say?