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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:52:54+00:00 2026-06-17T01:52:54+00:00

I have a model that has several attributes that are provided at creation. The

  • 0

I have a model that has several attributes that are provided at creation. The model also has some additional attributes that are derived from the provided attributes, which I also want to calculate at creation. More problematically, I want to be able to run validations on these derived values (since there are inputs that are valid on their own that lead to invalid derived values).

The problem is that when I do this:

class MyClass < ActiveRecord::Base
  attr_accessible :given1, :given2, :derived

  before_validation :derivation
  validates_uniqueness_of :derived

  def derivation
    self.derived = self.given1 + self.given2
  end
end

MyClass.new(:given1 => aNumber, :given2 => otherNumber)

I always get errors saying I can’t add nil to nil. Apparently self.attribute is nil until farther into the validation & creation process.

Obviously I could set my derived values in a later stage, and add a custom validation that works on the given attributes, but that would entail doing the derivation twice, which wouldn’t be very DRY.

Is there some other way to get at assigned but not yet validated attributes in the before_validates stage?

Edit: To clarify, I want to call MyClass.new(:given1 => aNumber, :given2 => otherNumber) and have the derived value calculated before the validations check, so that the validations check as if I had called MyClass.new(:given1 => aNumber, :given2 => otherNumber, :derived => aNumber + otherNumber). The problem is that I can’t seem to access the passed-in values for :given1 and :given2 in a before_validations method.

  • 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-17T01:52:56+00:00Added an answer on June 17, 2026 at 1:52 am

    I wrote my own snippet of code that looks like this:

    class User < ActiveRecord::Base
      attr_accessible :email, :first_name, :last_name
    
      validates :email, uniqueness: true
    
      before_validation :derivation
    
      def derivation
        self.email = self.first_name + self.last_name
      end
    end
    

    Running the following yielded no errors:

    »  u = User.new first_name: "leo", last_name: "correa"
    => #<User:0x007ff62dd8ace0> {
                          :id => nil,
                  :first_name => "leo",
                   :last_name => "correa",
                       :email => nil,
                  :created_at => nil,
                  :updated_at => nil,
    }
    »  u.valid?
      User Exists (0.9ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'leocorrea' LIMIT 1
    => true
    

    Running u.save saved the record successfully and upon repeating the User.new and saving that new record it returned with ROLLBACK because email was already used.

    In any case, make sure you are assigning whatever variables you are using to the given1, given2 and whatever the result is make sure is not giving you false either because it will cancel the before_validate callback and the record won’t save.

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

Sidebar

Related Questions

I have a view model that implements IValidatableObject and also has several validation attributes.
I have a view with a model that has several fields. When I render
I have a model (and model form based on it) that has several time
I have db.Model which has several properties as described below: class Doc(db.Model): docTitle =
I've a model kind of complicated. I have my UserViewModel which has several properties
Suppose I have a model Stock that has several StockPartition (its a property called
I have a model that has several properties. The properties can be primitive (String)
I have a model Articles that has_many Assets which is a polymorphic model that
I have a model that has a ForeignKey to the built-in user model in
I have a model that has an arbitrary number of children entities. For simplicity

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.