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

  • Home
  • SEARCH
  • 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

We have a database that persist our metadata and data. Our metadata is produced
I have an IScriptControl that needs to persist data from the user across postbacks.
We have created a control that needs to persist data via the ViewState property
I have a set of configuration items I need to persist to a human
I am trying to get LINQ to SQL to persist changes to an attached
I have an application that has many different types of objects that each persist
I'm using JPA (Hibernate's implementation) to annotate entity classes to persist to a relational
I'm trying to setup Spring using Hibernate and JPA, but when trying to persist
I am developing an iPhone application that persists data to a SQLite3 database. For

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.