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 6074307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:22:04+00:00 2026-05-23T10:22:04+00:00

@models.map(&:attributes)) returns a list of hashes from each column to its value in the

  • 0

@models.map(&:attributes)) returns a list of hashes from each column to its value in the db

How do I limit it so that only specific columns are returns (e.g. just name and id?).

Also, how do I combine multiple columns to a new key => value pair? For example, if a user has first_name and last_name, the above would return

[{"first_name" => "foo", "last_name" => "bar"}] but I want it to be [{"name" => "foo bar"}]

How do I achieve this transformation? Thanks!

  • 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-23T10:22:04+00:00Added an answer on May 23, 2026 at 10:22 am

    For the first part (limiting the attributes in the hash):

    @models.map {|model| model.attributes.slice(:id, :name)}
    

    For combining multiple attribute into a new attribute, the cleanest way is usually an accessor method:

    class User < ActiveRecord::Base
      def name
        "#{first_name} #{last_name}"
      end
    end
    

    Then build your hash manually during iteration:

    @models.map {|model| {:id => model.id, :name => model.name}}
    

    If you’re using more than one attribute from the attributes hash, you can use merge:

    @models.map do |model|
      model.attributes.slice(:id, :first_name, :last_name).merge(:name => model.name)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to maintain a list of unique models from a variety of queries.
This is my model: class Position(models.Model): map = models.ForeignKey(Map,primary_key=True) #members=models.CharField(max_length=200) LatLng = models.CharField(max_length=40000) infowindow
Two models (Rails 2.3.8): User; username & disabled properties; User has_one :profile Profile; full_name
I am using virtual attributes to save tags in an Entry model from a
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
This works: <li @{if (Model.Mode == map) {<text> class=bselected</text>}}>@Html.ActionLink(Map & Directions, MVC.Biz.Show(Model.SingleBiz.BizName, map, string.Empty))</li>
I have the following models & mappings (code snippets further below). One Competition has
I have a viewmodel called ArticleAdmin that includes a list of checkboxes: public class
I'm trying to make will_paginate's :order to work with this array: @posts = current_user.subscribed_tags.map(&:posts).flatten.paginate(:page
I have about 5 models that behave very similarly. In fact, I'd like them

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.