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

The Archive Base Latest Questions

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

I have a Rails model called Person which has database table columns for first_name

  • 0

I have a Rails model called Person which has database table columns for first_name and last_name. I’ve also defined a full_name method to return the combined first_name and last_name of the instance.

Is there a way to return an array, hash, or object which has first_name, last_name, as well as full_name?

Here’s the code I have:

#person.rb
class Person < ActiveRecord::Base
  validates_presence_of :first_name, :last_name  

  def full_name
        self.first_name + " " + self.last_name
  end
end

Here’s what I’ve tried in the Rails console:

ruby-1.8.7-p302 > person = Person.new({:first_name=>"Bruce", :last_name=>"Wayne"})
 => #<Person id: nil, first_name: "Bruce", :last_name: "Wayne", created_at: nil, updated_at: nil> 

ruby-1.8.7-p302 > person.save!
 => true 

ruby-1.8.7-p302 > Person.last
 => #<Person id: 1, first_name: "Bruce", :last_name: "Wayne", created_at: "2010-11-09 22:53:14", updated_at: "2010-11-09 22:53:14"> 

Is it possible to get something returned like this instead:

ruby-1.8.7-p302 > Person.last
 => #<Person id: 1, first_name: "Bruce", :last_name: "Wayne", :full_name: "Bruce Wayne", created_at: "2010-11-09 22:53:14", updated_at: "2010-11-09 22:53:14"> 

Or can it only return values from the database?

Eventually, I’d like to be able to call Person.all to return an array of hashes which also includes full_name.

Thanks in advanced!

  • 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-18T01:22:04+00:00Added an answer on May 18, 2026 at 1:22 am

    The first_name and last_name are DB attributes which are printed in the inspect method.
    If you want to access the full_name call full_name on the user object.

    User.last.full_name
    

    So if you want a hash of users with full_name as key:

    @users = {}
    User.all.each{|u| @users[u.full_name] = u}
    

    Edit 1

    The User.last call returns an User object. What gets printed in the console depends upon how the inspect method on the object is implemented. In case of ActiveRecord, database attributes are printed.

    If you need the full name, you need to call the full_name method on the returned User object.

    I am still not clear what you are trying to do.

    Edit 2

    u = User.last
    [u.full_name, u.age, u.phone] # array
    {:full_name => u.full_name, :age => u.age, :phone => u.phone } #hash
    

    Edit 3

    If you want to get a JSON format out of an object do the following

    u.to_json(:methods => [:full_name])
    

    Refer to the to_json documentation for more details.

    Edit 4

    The to_json method has include and exclude options for you to select the attributes you need. The to_json method gives you total control over data selection. Refer to the documentation link above for more details.

    u.to_json(:methods => [:full_name], :include => [:first_name, :last_name])
    

    If you need to include additional attributes, add them to the :include array in the above example.

    Edit 5

    To use this along with respond_with do the following:

    respond_with(@users, :methods => [:full_name], 
                         :include => [:first_name, :last_name])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app (running on version 2.2.2) that has a model called
I have a model Rails model called Orders that has a type_id, location, and
I have a rails application where each user has a separate database. (taking Joel
I'm writing a simple Rails model called Person that has_many :phone_numbers and I'm trying
I have a rails model called 'audioclip'. I orginally created a scaffold with a
In my rails model, I have a decimal property called employer_wcb. I would like
In my Rails app, I have a model called Photo and a PhotosController. As
I'm creating a Rails app, and I have a model called User . In
Let's say I have a controller and model called Car/Cars in Rails. Then I
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base

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.