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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:46:09+00:00 2026-05-29T10:46:09+00:00

In my application, I have a class called Budget. The budget can be of

  • 0

In my application, I have a class called Budget. The budget can be of many types.. For instance, let’s say that there are two budgets: FlatRateBudget and HourlyRateBudget. Both inherit from the class Budget.

This is what I get so far:

class Budget < ActiveRecord::Base
  validates_presence_of :price
end

class FlatRateBudget < Budget
end

class HourlyRateBudget < Budget
  validates_presence_of :quantity
end

In the console, if I do:

b = HourlyRateBudget.new(:price => 10)
b.valid?
=> false
b.errors.full_messages
=> ["Quantity can't be blank"]

As, expected.

The problem is that the “type” field, on STI, comes from params.. So i need to do something like:

b = Budget.new(:type => "HourlyRateBudget", :price => 10)
b.valid?
=> true

Which means that rails is running validations in the super-class instead of instantiating the sub class after I set up the type.

I know that is the expected behaviour, since I’m instantiating a class that dosen’t need the quantity field, but I wonder if there is anyway to tell rails to run the validations for the subclass instead of the super.

  • 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-05-29T10:46:10+00:00Added an answer on May 29, 2026 at 10:46 am

    You could probably solve this with a custom validator, similar to the answer on this question: Two models, one STI and a Validation However, if you can simply instantiate the intended sub-type to begin with, you would avoid the need for a custom validator altogether in this case.

    As you’ve noticed, setting the type field alone doesn’t magically change an instance from one type to another. While ActiveRecord will use the type field to instantiate the proper class upon reading the object from the database, doing it the other way around (instantiating the superclass, then changing the type field manually) doesn’t have the effect of changing the object’s type while your app is running – it just doesn’t work that way.

    The custom validation method, on the other hand, could check the type field independently, instantiate a copy of the appropriate type (based on the value of the type field), and then run .valid? on that object, resulting in the validations on the sub-class being run in a way that appears to be dynamic, even though it’s actually creating an instance of the appropriate sub-class in the process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a client application I have a DBIx::Class model 'Todo' that can be linked
In a C# Winforms (3.5) application I have added a class that contains many
I have a class called HeaderView which I use all over my application: public
I'm currently working on an OO PHP application. I have a class called validation
I have a class which is called a number of times. When the application
In my application I have a class which has properties of user-defined types like
I have a class that extends Application. The class is fairly extensive. When the
I have an application managing software tests and a class called TestResult: class TestResult
I have a class called SerialClient that sends/receives bytes over a serial port. When
I've written an application that creates a map activity. From there the user can

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.