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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:56:21+00:00 2026-05-19T02:56:21+00:00

I spent some time figuring this out and haven’t seen others post on it

  • 0

I spent some time figuring this out and haven’t seen others post on it so maybe this will help someone. Also, I don’t have much Rails experience so I’d be grateful for any corrections or suggestions, though the code below seems to work well.

I’ve set up a virtual attribute to make full_name out of first_name and last_name as discussed in the Railscast on virtual attributes . I wanted to search by full_name as well so I added a named_scope as suggested in Jim’s answer here.

named_scope :find_by_full_name, lambda {|full_name| 
  {:conditions => {:first => full_name.split(' ').first, 
     :last => full_name.split(' ').last}}
}

BUT… I wanted to be able to use all of this as :find_or_create_by_full_name. Creating a named scope with that name only provides searching (it’s identical to the :find_by_full_name code above) — i.e. it doesn’t do what I want. So in order to handle this I created a class method for my User class called :find_or_create_by_full_name

# This gives us find_or_create_by functionality for the full_name virtual attribute.
# I put this in my user.rb class.
def self.find_or_create_by_full_name(name)
  if found = self.find_by_full_name(name).first # Because we're using named scope we get back an array
    return found
  else
    created = self.find_by_full_name(name).create
    return created
  end
end
  • 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-19T02:56:22+00:00Added an answer on May 19, 2026 at 2:56 am

    You could as well just Use
    User.find_or_create_by_first_name_and_last_name(:first_name => "firstname", :last_name => "last_name")

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

Sidebar

Related Questions

I spent some time trying to figure out why this query isn't pulling the
I've spent some time researching this and still can't figure it out. It seem
I've spent some time on Google and SO trying to figure this out and
I spent some time this morning trying to help a colleague with an odd
I've spent some time today looking for a pure ruby library that will parse
I'm not quite sure what this is called, but I've spent some time thinking
So I've spent some time looking for an answer for this, but so far
I spent some time checking out JBoss AS7 today. I am impressed with what
So I've spent some time checking out CocoaDev, reading the Cocoa docs on NSMenuItems,
hey guys, i spent some time on this one but i find it really

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.