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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:30:22+00:00 2026-05-16T01:30:22+00:00

I need help on what I think should be an ActiveRecord Callback. What I

  • 0

I need help on what I think should be an ActiveRecord Callback.

What I am trying to achieve is, each time a particular record is saved, ActiveRecord actually saves 2 records in the same table.

I will stay away from why I want this, it should be good enough to know that each transaction needs 2 separate but similar records saved in the same table.

Example:

0) I have a table called “links” which will store people linked to other people. It looks like this:

+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+

1) The web app user will enter in the originating person and the receiving person (who is originating or receiving the link). At this point the “person_id” is blank:

+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1  |           | 5             | 10          |
+----+-----------+---------------+-------------+

2) Before the record saves, I need the value of the “origin_person” copied over to the “person_id column:

+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1  | 5         | 5             | 10          |
+----+-----------+---------------+-------------+

3) Finally, once the record has been saved, I need a duplicate record saved, but with the “rcvd_person” value copied over to the “person_id”:

+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1  | 5         | 5             | 10          |
+----+-----------+---------------+-------------+
| 2  | 10        | 5             | 10          |
+----+-----------+---------------+-------------+

This is exactly what I am trying to do, so if you can help me with an idea of how this could be achieved that would be appreciated.

I would rather keep all of this out of the Rails View, as I don’t want hidden fields and what not hanging around.

One problem I ran into in trying to solve this, was that it would loop. I wrote some code (that didn’t work) that would create a new record once a record was saved, but each save would trigger a new record which was a new save which ….

  • 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-16T01:30:23+00:00Added an answer on May 16, 2026 at 1:30 am

    You can add a flag to your model which could be used to prevent row duplication. That way when you create a new entry it gets saved after_* callback is called and in it you create a new object and set the duplication flag to prevent further duplication.

    Something along the lines of:

    def Link
      after_create :duplicate_link
      attr_accessible :stop_duplication
    
      private
      def duplicate_link
        unless stop_duplication
          new_link = Link.new
          # set all the link attrs here
          new_link.stop_duplication = true
          new_link.save
        end
      end
    end
    

    (of course this code is not tested, I just wanted to illustrate how it can be done)

    That being said, I would really think hard why and if I need this type of behavior. It seems to me that something else might be wrong if you need this.

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

Sidebar

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.