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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:43:57+00:00 2026-05-13T14:43:57+00:00

Is there a method to save an object but return the object if it

  • 0

Is there a method to save an object but return the object if it fails the validation_uniqueness_of for a given field? For example, I have the following:

class User
  has_many :words
...
end

class Word
  belongs_to :user
  validates_uniqueness_of :title
...
end  

And I have a situation where I want to either return the Word object if the user has already saved this word, or return the newly saved word object. I am finding myself checking if the word object exists through my own search and then performing a save if not:

existing_word = find_by_word_and_user_id(word, user)
!existing_word.nil? ? existing_word : user.words.create({:word => word})

But I feel like if the user does not have the word saved, rails is going to perform a redundant search for the title uniqueness validation after my check and I was wondering if I could just do the save and return the already existing object that rails finds for the validation.

  • 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-13T14:43:57+00:00Added an answer on May 13, 2026 at 2:43 pm

    You might be interested in #find_or_create_by_word_and_user, another of ActiveRecord’s magic methods:

    obj = Word.find_or_create_by_word_and_user(word, user)
    
    # Preferred way
    obj = user.words.find_or_create_by_word(word)
    

    This will do 2 database requests anyway: there’s no way around that. Also, if your Word instance has more validation that you’ve shown us, the create will fail, because #find_or_create_by does not allow passing more attributes.

    For all the details on the dynamic finder methods, search for “Dynamic attribute-based finders” on http://api.rubyonrails.org/classes/ActiveRecord/Base.html.

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

Sidebar

Related Questions

Let say I have the following desire, to simplify the IConvertible's to allow me
There doesn't seem to be any tried and true set of best practices to
Is there a way to test if a collection is already initialized? try-catch only?
If all tables I want to delete from have the column gamer_id can i
We manage a site for a medical charity. They have a number of links
This is beyond both making sense and my control. That being said here is
I would like to remove/delete a migration file. How would I go about doing
I am attempting to pull some information from my tnsnames file using regex. I
I've found several jQuery syntaxes for nullifying the enter on a form. First one:
I notice in several API's, that you may create a struct which is used

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.