Skip to content

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.

View the snippets archive ››

No comments have been left

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com