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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:55:51+00:00 2026-06-07T11:55:51+00:00

I have run into an issue and I think that my solution is very

  • 0

I have run into an issue and I think that my solution is very ugly at the moment, what is a better way I can do the following with rails/mongoid? Basically, a user can come in and provide a ‘nil’ answer_id, but as soon as they answer the question, we want to lock in their first, non-nil answer.

controller.rb

r = Response.new(user: current_user, question_id: qid, answer_id: aid)
r.save_now!

And the following response.rb model:

def save_now!
    user = self.user
    qid = self.question_id
    aid = self.answer_id
    resp = Response.where({user_id: user._id, question_id: qid}).first

    # We accept the first answer that is non-nil, 
    # so a user can skip the question (answer_id=nil)
    # And then return and update the answer_id from nil to 'xyz'
    if resp.nil?
      resp = Response.new(user: user, question_id: qid, answer_id: aid)
    else
      if resp.answer_id.nil? && aid.present?
         resp.answer_id = aid
      end    
    end    
    resp.save!
end

So I would like to allow for answer_id to be nil initially (if a user skipped the question), and then take the first answer that is non-nil.

I really don’t think it’s intuitive and clean to instantiate the Response object twice, once in controller and once in model but I’m not sure on the best way to do this? Thanks.

  • 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-07T11:55:53+00:00Added an answer on June 7, 2026 at 11:55 am

    Create a unique index over (user, question_id, answer_id). This way only the first insert will succeed. Subsequent inserts will fail with an error. This eliminates the need for the find query in your save_now! method.

    Remember to run this insert in safe mode, or else you won’t get an exception, it will just fail silently.

    Update

    Seems that your problem might be solved by renaming the method. 🙂 Take a look:

    class Response
      def self.save_answer(user, qid, aid)
          resp = Response.where({user_id: user._id, question_id: qid}).first
    
          if resp.nil?
            resp = Response.new(user: user, question_id: qid, answer_id: aid)
          else
            if resp.answer_id.nil? && aid.present?
               resp.answer_id = aid
            end    
          end    
          resp.save!
      end
    end
    
    # controller.rb
    r = Response.save_answer(current_user, qid, aid)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have run into an issue that i am hoping somebody can help give me
I have run into an issue where an input field within a form I'm
I am creating a Node.js app on Heroku and have run into an issue.
I am creating my first (!) database, and I have run into an issue
I have run into a small issue in my program. I have a class
I have run into a bit of a design issue with my code. I
Trying to solve an issue for someone else and instead have run into my
I have recently run into a particularly sticky issue regarding committing the result of
I have run into a little problem. I am connecting to a webservice that
I've run into the following issue which is not difficult to solve by any

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.