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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:24:40+00:00 2026-06-18T14:24:40+00:00

I have a Rails model that has a belongs_to relationship to another model. class

  • 0

I have a Rails model that has a belongs_to relationship to another model.

class Foo < ActiveRecord::Base
  belongs_to :bar
end

Every time a bar is set / updated on the foo I want “intercept” the modification of that change and update something on the foo.

My guess is that I can just override the setter for bar and do what I want. But, since I’m relatively new to Ruby / Rails, my concern is that there are “magical” ways that the bar can get set on the foo that don’t go through my setter (perhaps with mass-assignment, update/update_all calls, etc).

What is the safest way to ensure that any change to bar runs a block of code that reacts to that change?

  • 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-18T14:24:42+00:00Added an answer on June 18, 2026 at 2:24 pm

    Depending on your needs, you can use callbacks on your bar to execute specific code on your foo.

    All the documentation you need is here http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

    For example :

    class Bar < ActiverRecord::Base
        has_many :foos
        before_save :do_something
    
        private
            def do_something
                self.foos.each {|f| f.update_attribute('bar_has_changed', true) }
            end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Rails if you have a model Walrus that has_many :bubbles (and Bubble belongs_to
I have a Ruby on Rails model that has a column called expiration_date .
I have a rails app (rails 3.1.3) that has a shopping cart model. I
I have a Rails engine that defines a class method top(count) on a model
Given that I have a Foo model w/ the standard Rails timestamp columns what
I have a rails app (running on version 2.2.2) that has a model called
I have a Vehicle model: class Vehicle < ActiveRecord::Base validates_presence_of :num_of_wheels validates_presence_of :make validates_presence_of
My rails app has a user model that has a HABTM relationship with interests
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
i have the following code in the rails company model: class Company include Mongoid::Document

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.