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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:50:51+00:00 2026-05-13T06:50:51+00:00

How do you persist a derived attribute which depends on the value of id

  • 0

How do you persist a derived attribute which depends on the value of id in rails? The snippet below seems to work– Is there a better rails way?

class Model < ActiveRecord::Base
  ....
  def save
    super
    #derived_attr column exists in DB
    self.derived_attr = compute_attr(self.id)
    super
  end
end
  • 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-13T06:50:52+00:00Added an answer on May 13, 2026 at 6:50 am

    Callbacks are provided so you should never have to override save. The before_save call in the following code is functionally equivalent to all the code in the question.

    I’ve made set_virtual_attr public so that it can be calculated as needed.

    class Model < ActiveRecord::Base
      ...
      # this one line is functionally equivalent to the code in the OP.
      before_save :set_virtual_attr
      attr_reader :virtual_attr
    
      def set_virtual_attr
        self.virtual_attr = compute_attr(self.id)
      end
      private
      def compute_attr
        ...
      end  
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to persist a User-class, which contains 2 lists: one with users to
I have a class RequestDetail . There are 2 classes classA and classB derived
I'm looking to persist and retrieve a large amount of key-value pair(s) type data
I need to persist an object that is not marked with the serializable attribute.
I have sets of entities all of them are derived from abstract class public
One Update below I have the following class that derives from EqualityComparer<T> that I
I have 3 non-abstract persist-able classes. MyClubUser and HisClubUser classes inherit from User class.
I'm trying to persist a very simple class using Hibernate. I'm not using Spring,
I have a class MyMainApplication which is a SPIisWebServiceApplication. So it is hosted and
Can a class inherit from Entity Framework class and still persist back to the

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.