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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:32:29+00:00 2026-06-10T18:32:29+00:00

This is in Ruby 1.9.3p194, with Rails 3.2.8 app/models/owner.rb: class Owner < ActiveRecord::Base attr_accessible

  • 0

This is in Ruby 1.9.3p194, with Rails 3.2.8

app/models/owner.rb:

class Owner < ActiveRecord::Base
  attr_accessible :name
  has_many :pets
end

app/models/pet.rb:

class Pet < ActiveRecord::Base
  attr_accessible :name, :owner
  belongs_to :owner
end

db/migrate/20120829184126_create_owners_and_pets.rb:

class CreateOwners < ActiveRecord::Migration
  def change
    create_table :owners do |t|
      t.string :name
      t.timestamps
    end
    create_table :pets do |t|
      t.string :name
      t.integer :owner_id
      t.timestamps
    end
  end
end

Alright, now watch what happens…

# rake db:migrate
# rails console
irb> shaggy = Owner.create(:name => 'Shaggy')
irb> shaggy.pets.build(:name => 'Scooby Doo')
irb> shaggy.pets.build(:name => 'Scrappy Doo')
irb> shaggy.object_id
  => 70262210740820
irb> shaggy.pets.map{|p| p.owner.object_id}
  => [70262210740820, 70262210740820]
irb> shaggy.name = 'Shaggie'
irb> shaggy.name
  => "Shaggie"
irb> shaggy.pets.map{|p| p.owner.name}
  => ["Shaggie", "Shaggie"]
irb> shaggy.save
irb> shaggy.reload
irb> shaggy.object_id
  => 70262210740820
irb> shaggy.pets.map{|p| p.owner.object_id}
  => [70262211070840, 70262211079640]
irb> shaggy.name = "Fred"
irb> shaggy.name
  => "Fred"
irb> shaggy.pets.map{|p| p.ower.name}
  => ["Shaggie", "Shaggie"]

My question: How can I get rails to initialize the elements of shaggy.pets to have their owners set to shaggy (the exact object), not only when the pet objects are first built/created, but even when they are auto-loaded from the database via the association?

Bonus points: Make it work in Rails 2.3.5 as well.

  • 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-10T18:32:31+00:00Added an answer on June 10, 2026 at 6:32 pm

    Figured it out. This works for me in Rails 3.2.8 and Rails 2.3.5:

    class Owner < ActiveRecord::Base
      attr_accessible :name
      has_many :pets do
        def load_target
          super.map do |pet|
            pet.owner = (proxy_association.owner rescue proxy_owner)
            pet
          end
        end
      end
    end
    

    Note that in Rails 2.3.5, the object_ids of shaggy.pets.map{|p| p.owner} are still different, since they are different ProxyAssociation objects, but they at least point at the same underlying object.

    If you do this often, you may want to generalize…

    lib/remember_parent_extension.rb:

    class RememberParentExtension < Module
      def initialize(parent_name)
        parent_setter = "#{parent_name}="
        super() do
          define_method(:load_target) do
            super.map do |child|
              parent_proxy = (proxy_association.owner rescue proxy_owner)
              child.send(parent_setter, parent_proxy)
              child
            end
          end
        end
      end
    end
    

    app/models/owner.rb:

    class Owner < ActiveRecord::Base
      attr_accessible :name
      has_many :pets, :extend => RememberParentExtension.new(:owner)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain me this Ruby on Rails puzzle? class Post < ActiveRecord::Base has_many
In this Ruby code: Module M Class C < Struct.new(:param) def work M::helper(param) end
I have this ruby script that allows me to enter the name of a
Referencing this post: ruby on rails average per day I'm getting the average per
I'm having a little problem with this Ruby code: class Tap attr_accessor :c, :v,
Environment: Rails 3.2.3 Mac OS X Lion Ruby-1.9.3-p194 (MRI) I have a rails app
I am running a Rails (3.2.3) application with Ruby 1.9.3p194 on the basic Ubuntu
bitnami@ip-10-99-67-123:~/.rvm/gems$ ruby -v ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] bitnami@ip-10-99-67-123:~/.rvm/gems$ rails -v Rails 3.2.3
Vitals: ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux] Rails 3.2.7 Devise 2.1.2 The GET /users/sign_in
I am running Rails 3.2.8 and ruby 1.9.3p194. I'm trying to better understand exactly

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.