1

So I have the following code that I wanted to make a little more readable:

user = User.find_by_username(username)
user = User.new(credentials) if user.nil?

Essentially the first line is the default, the following line instantiates itself if it's not already set.

Is there a way I can clean this up a bit and get it onto a single line of code? Something like this maybe:

user = User.find_by_username(username) || User.new(credentials)

Edit

I now realize the the code I provided does exactly what I need, sorry for wasting cyberspace but feel free to suggest alternative solutions.

3
  • 1
    What's wrong with exactly the line you suggest? Commented Oct 12, 2012 at 22:11
  • 1
    Oh, what do you know that pseudo code actually worked Commented Oct 12, 2012 at 22:15
  • 1
    That really is a demonstration of the expressivity of the language. Commented Oct 12, 2012 at 22:36

2 Answers 2

5

Yes, what you wrote is exactly the correct answer.

You could also write it this way, if preferred:

user = User.find_by_username(username)
user ||= User.new(credentials)

Keep in mind that your first example will only assign the second value if the first one is nil, but your second example and my example above will assign the second value on both nil and false.

EDIT: As mentioned by others, if you're working with Active Record you could also use a dynamic attribute-based finder

user.find_or_initialize_by_username(credentials)
Sign up to request clarification or add additional context in comments.

Comments

3

its easier to do

user.find_or_create_by_name(credentials)

and credentials will have username there

see Rails find_or_create by more than one attribute?

and http://api.rubyonrails.org/classes/ActiveRecord/Base.html

2 Comments

Good thinking, but in this case I'm not looking to commit the object right off the hop.
Then you could use user.find_or_initialize_by_username(credentials)... see api.rubyonrails.org/classes/ActiveRecord/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.