I am trying to get a collection of objects based on a conditions. Now normally in C# I would do something like this
employeesCollection.Where(emp => emp.Name == "john");
how can I do something similar in Ruby on Rails (I am trying to map a collection of objects to a select but I only want to map certain objects that match a condition.
My current ruby on rails code looks like this
<%= select( 'page', 'id', @post.pages.map {|page| [page.title, page.id]}) %>
I want to add a condition to an attribute of page
Can anyone help?
You can just throw a
selectblock in there beforemap:A synonym for select is
find_all.As you probably guessed,
selectin Ruby is approximately equivalent to LINQ’sWhere. Select takes a block, and each element in yourEnumerableis passed to that block; when the block returns true (non-false, non-nil), then that element isselected.The antonym for
selectisreject.rejectis preferred when your select is negative: that is to say,is less preferable than