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

  • Home
  • SEARCH
  • 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 8766831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:38:53+00:00 2026-06-13T16:38:53+00:00

The code smells and i’m sure i’m missing a helper to do a simple

  • 0

The code smells and i’m sure i’m missing a helper to do a simple
update or create query like the below snippet :

  @user_answer = current_user.user_answers.find_by_question_id(params[:a_question])
  if @user_answer.nil?
    @user_answer = current_user.user_answers.build({ :answer_id => params[:an_answer] , :question_id => params[:a_question] })      
    @user_answer.save  
  else
    @user_answer.update_attributes(:answer_id => params[:an_answer])
    @user_answer.save 
  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-06-13T16:38:55+00:00Added an answer on June 13, 2026 at 4:38 pm

    You might be looking for the find_or_initialize_by method. For example:

    @user_answer = current_user.user_answers.find_or_initialize_by_question_id(params[:a_question])
    @user_answer.answer_id = params[:an_answer]
    @user_answer.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a bubble sort in Prolog (code below). It works but it smells.
Code to create new form instance of a closed form using form name I
This code smells... how do I rewrite it better? my $record; eval { while
Model Item belongs_to User . In my controller I have code like this: @items
I have code that looks like follows: public interface BaseDAO{ // marker interface }
We're generally familiar with code smells here, but just as damaging if not more
I want to create an extension to recognize specific comments in my code. I
Working on existing code, I recently found smt like this: if(Config.ExceptionBehavior.DEBUG_EXCEPTIONS) { foo.Foo(); }
I would like to run some unit tests on the method below I am
I'm looking for a piece of code which behaves a bit like a singleton

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.