Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:16:06+00:00 2026-05-15T06:16:06+00:00

I am trying to get a collection of objects based on a conditions. Now

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:16:07+00:00Added an answer on May 15, 2026 at 6:16 am

    You can just throw a select block in there before map:

    >> [1,2,3,4,5].select { |x| x.odd? }.map{ |x| x*x }
    => [1, 9, 25]
    

    A synonym for select is find_all.

    As you probably guessed, select in Ruby is approximately equivalent to LINQ’s Where. Select takes a block, and each element in your Enumerable is passed to that block; when the block returns true (non-false, non-nil), then that element is selected.

    The antonym for select is reject. reject is preferred when your select is negative: that is to say,

    ary.select {|x| x != 'foo'}
    

    is less preferable than

    ary.reject {|x| x == 'foo'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this answer Get the subdomain from a URL , I am trying
So I'm trying to find a way to get a collection of all versions
I'm getting the error Illegal access to loading collection when I'm trying to get
Im trying to select from a collection in linq based on an id on
For about a week now I have been trying to get a view to
So I'm trying to learn how to implement method collection for a plugin based
Hi I am trying to get a connection from windows a unix box and
I am trying to get the IP of a socket connection in string form.
I am currently trying to get my server to create a connection with a
I'm trying to get to grips with regular expressions: I have a database connection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.