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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:55:49+00:00 2026-05-11T15:55:49+00:00

I need to do conditional validation of models in Rails dependant on one of

  • 0

I need to do conditional validation of models in Rails dependant on one of the fields within the model. This is a follow-up to an earlier question which prompted development of a solution that doesn’t feel right.

I have a number of ActiveRecord models all based around an ‘Order Model’, a shortened version of which is shown below.

class Order < ActiveRecord::Base    has_many: previous_addresses   has_one: applicant   has_one: current_address end 

All of the other models belong to order.

I wish to validate the various models such as applicant, current_address etc differently based upon the company attribute of the order.

I have developed a solution to this which involved a complete hand-rolled validation suite, supporting all of the standard activeRecord validators. This solution was developed with a number of tables such as model, fields, field_items etc that described the validation and amounts to quite a lot of data.

I now have to manipulate this data and copy and edit it for each new company that I am connecting to. This is tedious and error prone.

Whilst reading ‘The Rails Way’, I found a section where Obie Fernandez described keeping the validation code in the database in the form of ActiveRecord validation statements and injecting these into the models at runtime based upon the value of the company id.

I am imagining keeping data in tables such as the following :-

 company: 200 model: person Code validates_length_of :middle_name, :maximum => 20, :allow_nil => true  company: 201 model: person Code validates_length_of :middle_name, :maximum => 1, :allow_nil => true 

So for company 201 a middle name of ‘John’ would fail validation but that would be ok for company 200.

Has anyone seen this done, and if so do they have any tips/suggestions on how to do it, even better does anyone have any links to tutorials/ code that does this?

  • 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. 2026-05-11T15:55:49+00:00Added an answer on May 11, 2026 at 3:55 pm

    It sounds like what you need is the ability to validate each record separately. If this is true then you could use validates_each. In this block you can pass in logic to evaluate the members of your model.

     validates_each :middle_name do |record, attr, value|    if record.company == 201      record.errors.add attr, 'cannot be more than 1 character' if value.length > 1    elsif record.company == 200      record.errors.add attr, 'must be less than 20 characters' if value.length > 1    end  end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two validation jQuery functions which I need to fire one after another.
During one of my project i need to add required field validation on the
I need a conditional compilation switch that knows if I am compiling for the
I need the the .ASPX file to behave differently based on a conditional compilation
HI, I need to write multiple statements inside a conditional operator. What will be
I need some help adding conditions to a linq query. Take this small example
I have a condition in which I need to close the application and so
Problem: In my model, I need to use callbacks to process the db entity
I need your help to understand this better. This is my case. I have
Below is my validation() function which controls some messages in an alert: function validation()

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.