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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:48:07+00:00 2026-06-13T11:48:07+00:00

I have a very generic validator and I want to pass it arguments. Here

  • 0

I have a very generic validator and I want to pass it arguments.

Here is an example model:

class User
  include Mongoid::Document

  field :order_type
  has_many :orders, inverse_of :user
  validates: orders, generic: true #i want to pass argument (order_type)

  field :task_type
  has_many :tasks, inverse_of :user
  validates: tasks, generic: true #i want to pass argument (task_type)
end

and Example validator:

class GenericValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    if some_validation?(object)
      object.errors[attribute] << (options[:message] || "is not formatted properly") 
    end
  end
end

Is there any way to pass arguments to the validator dependant on which field it is validating?

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-13T11:48:08+00:00Added an answer on June 13, 2026 at 11:48 am

    I wasn’t aware of this either, but if you want to pass an argument, then pass a hash to generic: instead of true. This post details the exact process you’re wanting to follow:

    class User
      include Mongoid::Document
    
      field :order_type
      has_many :orders, inverse_of :user
      validates: orders, generic: { :order_type => order_type }
    
      field :task_type
      has_many :tasks, inverse_of :user
      validates: tasks, generic: { :task_type => task_type }
    end
    

    GenericValidator should now have access to both arguments you’re wanting to pass in validation: options[:order_type] and options[:task_type].

    It might make more sense, however, to divide these up into two validators, with both inheriting the shared behavior as mentioned by dpassage:

      class User
        include Mongoid::Document
    
        field :order_type
        has_many :orders, inverse_of :user
        validates: orders, order: { type: order_type }
    
        field :task_type
        has_many :tasks, inverse_of :user
        validates: tasks, task: { type: task_type }
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a very generic question but here it goes. I have
This is a very generic question. I have a hashmap and I want to
I have a very generic login form created that allows a user to access
Suppose I have written a decorator that does something very generic. For example, it
I have a very hard-coded XML reader below: using System; using System.Collections.Generic; using System.Linq;
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/ As
I have a very generic set of three tables that store all of my
At present, I have very successfully architected my applications as follows: Data model (Entity
I have a very generic question. When Developing with ASP.NET MVC. It provides a
Before starting, I do have a very particular question and if you want to

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.