# How to find parent object\_id into child object?

**URL:** <https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412>\
**Category:** Ruby\
**Created:** [February 18, 2008, 12:18pm UTC](https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412 "2008-02-18T12:18:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 18, 2008, 12:18pm UTC](https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412/1 "2008-02-18T12:18:16Z")

</div>

suppose a object (obj1) have another object (obj2) itself.  
how to find obj1.object\_id inside obj2 ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 18, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412/2 "2008-02-18T13:40:18Z")

</div>

2008/2/18, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> suppose a object (obj1) have another object (obj2) itself.  
> how to find obj1.object\_id inside obj2 ?

You can’t. Well, you can try but there is no guarantee that you will  
find all obj1 that reference obj1 because it won’t work for Structs:

irb(main):001:0\> S=Struct.new :obj  
=\> S  
irb(main):002:0\> s1 = S.new  
=\> #  
irb(main):003:0\> s2 = S.new s1  
=\> #\<struct S obj=#\>  
irb(main):004:0\> ObjectSpace.each\_object(Object) do |obj|  
irb(main):005:1\* puts obj.object\_id if  
irb(main):006:1\* obj.instance\_variables.any? {|iv|  
obj.instance\_variable\_get(iv).equal? s1}  
irb(main):007:1\> end  
=\> 4228  
irb(main):008:0\> s2.object\_id  
=\> 1073522830  
irb(main):009:0\> s2.instance\_variables  
=\> []  
irb(main):010:0\>

If obj2 needs to know, you should explicitly tell it.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 18, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412/3 "2008-02-18T17:26:00Z")

</div>

Well, I’m not sure if I understood the question right. But I think  
something like this is possible (struct maybe is a bad choice for  
demonstration purposes):

class A  
def initialize(o=nil)  
@o = o  
end  
end

Anyway, the proper way probably would be to design the class so that  
obj2 gets a reference to obj1 when it is included in obj1 (if you know  
it needs such a reference), which is what Robert meant I suppose.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 19, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/how-to-find-parent-object-id-into-child-object/129412/4 "2008-02-19T15:52:10Z")

</div>

ThoML wrote:

> Well, I’m not sure if I understood the question right. But I think  
> something like this is possible (struct maybe is a bad choice for  
> demonstration purposes):
> 
> class A  
> def initialize(o=nil)  
> @o = o  
> end  
> end
> 
> Anyway, the proper way probably would be to design the class so that  
> obj2 gets a reference to obj1 when it is included in obj1 (if you know  
> it needs such a reference), which is what Robert meant I suppose.

i am a beginner rails developer

in rails archies there is no proper relation between Model and  
Controller

inside controller i should create Model object .  
At such situation i want to assign some values to model\_object  
attributes from controller (without argument passing to model\_object ) .  
like

nowadays i am doing like this  
model\_object=Record.find(:first)  
model\_object.updated\_by=session[:user]  
model\_object.save

but i want like this

class Record \< AR  
before\_update :assign\_updated\_by  
protected  
def assign\_updated\_by  
self.updated\_by=session[:user] #or some idea here  
end  
end  
model\_object=Record.find(:first)  
model\_object.save  
…  
any idea…
