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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:08:35+00:00 2026-06-09T23:08:35+00:00

Using Mongoid, I am trying to validate the :code input on the submission form

  • 0

Using Mongoid, I am trying to validate the :code input on the submission form to make sure they are using a proper code that is already stored in the database. There are about 2000+ codes so a helper method array collection wouldn’t be feasable.

What is the best way to do this?

I was thinking of doing an inclusion validation, like this:

class Request
include Mongoid::Document 
field :code, type: String      
validates :code, :presence => true, 
                 :inclusion => { :in => proc { Listing.all_codes } }
end

Then the model that has all the stored codes, like this:

class Listing
include Mongoid::Document
field :code, type: String 

  def self.all_codes
    where(:code => exists?) # <--- this is broken
  end
end

But I can’t seem to get this to function the way I would like. Any feedback would be much appreciated.

  • 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-09T23:08:36+00:00Added an answer on June 9, 2026 at 11:08 pm

    Your Request model looks fine. But the Listing.all_codes needs to return an array of only codes. so:

    class Listing
      include Mongoid::Document
      field :code, type: String 
    
      def self.all_codes
        only(:code).map(&:code)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MongoDB and trying to make sure I set up the proper
I am using Mongoid on Rails 3 and trying to build a nested form.
I'm trying to use the jQuery token input plugin with Rails and Mongoid. Using
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I've got a Ruby on Rails app that is using MongoDB for datastorage. Mongoid::Timestamp
I was writing a Blog like application Using Rails3/Mongoid, and now trying to use
I'm using Mongoid for a project and am trying to use the batch insert
I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling
I am trying to initialise mongoid from a rails 3.2 application using ruby-1.9.2 with
I'm trying to execute a more or less advanced query with Mongoid that basically

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.