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

  • Home
  • SEARCH
  • 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 8722665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:27:47+00:00 2026-06-13T07:27:47+00:00

I’m currently busy learning Ruby and Rails, and since I have a background in

  • 0

I’m currently busy learning Ruby and Rails, and since I have a background in C-based languages, some concepts of Ruby are new and somewhat alien. Especially challenging for me is adapting to the “Ruby-way” of approaching common problems, hence I often find myself coding C in Ruby, which is not what I’m trying to achieve.

Imagine having a schema like this:

ActiveRecord::Schema.define(:version => 20111119180638) do
    create_table "bikes", :force => true do |t|
        t.string   "Brand"
        t.string   "model"
        t.text     "description"
    end
end

The database already contains several different bikes. My goal is to get an array of all brands represented in the database.

Here is my code:

class Bike < ActiveRecord::Base
    def Bike.collect_brands
        temp_brands = Bike.find_by_sql("select distinct brand from bikes")
        brands = Array.new
        temp_brands.each do |item|
          brands.push(item.brand)
        end
        brands
    end
end

How would a Ruby guru write code to achieve this?

  • 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-06-13T07:27:48+00:00Added an answer on June 13, 2026 at 7:27 am

    tl;dr: Your entire method can be replaced with Bike.uniq.pluck(:brand).


    This functionality already exists (see the end of my answer), but first, lets step through your code and make it more idiomatic:

    First and foremost, use two spaces per level of indentation, not four, not eight, and not tabs. Use two spaces. This is not personal preferences, this is an extremely strong convention within the Ruby community and pretty much required if you intend to participate.

    Next, there almost never a good reason to use this pattern in Ruby:

     brands = Array.new
     temp_brands.each do |item|
       brands.push(item.brand)
     end
    

    When you want to translate one array into another array (really, one Enumerable into another Enumerable) by applying some code to each of values in the input array, use map or collect (which are synonyms):

    brands = temp_brands.map { |item| item.brand }
    

    Next, you can take advantage of symbol#to_proc to make the above code a little clearer:

    brands = temp_brands.map &:brand 
    

    This will look strange to the uninitiated, but it is clearer once you’re used to working with map and &:field. A little bit of experience will make the intent of this line of code very obvious: It’s applying the brand method to each element in the array, and it’s exactly equivalent to the previous { |item| item.brand } version.

    Now, your entire method can become a pretty simple one-liner:

    def Bike.collect_brands
      Bike.find_by_sql("select distinct brand from bikes").map &:brand
    end
    

    That inline select/distinct SQL is kind of ugly, especially since ActiveRecord already lets us select specific fields with select, and make the results distinct using uniq:

    def Bike.collect_brands
      Bike.select(:brand).uniq.map &:brand
    end
    

    As a final iteration we can use pluck instead of map to pull only the fields out of the results that we’re interested in. But, because pluck actually modifies the SQL being generated to only include the fields being plucked, we can omit the select(:brand) portion, and our code boils down to an incredibly short single line containing two chained methods:

    def Bike.collect_brands
      Bike.uniq.pluck(:brand)
    end
    

    Note that the order is important because pluck always returns an array, not an ActiveRecord relation ready for additional method chaining. Bike.pluck(:brand).uniq would select the brand from every record (select brand from bikes) and then, in Ruby, reduce the array to the unique items. Potentially a very expensive operation.

    And that’s it, Bike.uniq.pluck(:brand). As a C programmer, you’ll find that many of repetitive tasks you’re used to doing with small loops are practically already solved for you by the language itself or by supporting libraries. The amount of code you don’t write can be very surprising, once you’ve learned to write idiomatic Ruby and Rails code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.