Hi, I am having difficulties accessing parent properties/attributes from the child. Anyone could shed some lights onto this? Heres a sample code: class Parent < ActiveRecord::Base :has_many => :childs @connector def before_save @connector = ''some connector object'' 0..9.do |count| self.childs << Child.new self.childs[count].connector = @connector end end end class Child < ActiveRecord::Base :belongs_to => :parent @connector def before_save # code goes here to access @connector end end The above is just a recreation of what I am looking to do. So basically I want to be able to access objects like the connector from the child, or even the parent itself by something like self.parent.attributes. Any ideas? thanks, nayeem
Hi, when I saw your models, I was thinking that you should have the following relationships: Parent has many children Child has many parents Thus, you should have the following database tables: children and parents. By doing this, this becomes a many to many relationship. Good luck, -Conrad Sent from my iPhone On Jun 7, 2009, at 1:35 AM, developerinlondon <ebillionaire-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I am having difficulties accessing parent properties/attributes from > the child. Anyone could shed some lights onto this? > > Heres a sample code: > > class Parent < ActiveRecord::Base > :has_many => :childs > @connector > def before_save > @connector = ''some connector object'' > 0..9.do |count| > self.childs << Child.new > self.childs[count].connector = @connector > end > end > end > > class Child < ActiveRecord::Base > :belongs_to => :parent > @connector > def before_save > # code goes here to access @connector > end > end > > The above is just a recreation of what I am looking to do. So > basically I want to be able to access objects like the connector from > the child, or even the parent itself by something like > self.parent.attributes. > > Any ideas? > > thanks, > > nayeem > > >
No I need to have it so that Child has one parent. I managed to get a reference to the parent by using the ObjectSpace. But its almost a hack then a nice way of doing it. 2009/6/7 Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > Hi, when I saw your models, I was thinking that you should have the > following relationships: > > Parent has many children > > Child has many parents > > Thus, you should have the following database tables: children and > parents. By doing this, this becomes a many to many relationship. > > Good luck, > > -Conrad > > Sent from my iPhone > > On Jun 7, 2009, at 1:35 AM, developerinlondon <ebillionaire-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > Hi, > > > > I am having difficulties accessing parent properties/attributes from > > the child. Anyone could shed some lights onto this? > > > > Heres a sample code: > > > > class Parent < ActiveRecord::Base > > :has_many => :childs > > @connector > > def before_save > > @connector = ''some connector object'' > > 0..9.do |count| > > self.childs << Child.new > > self.childs[count].connector = @connector > > end > > end > > end > > > > class Child < ActiveRecord::Base > > :belongs_to => :parent > > @connector > > def before_save > > # code goes here to access @connector > > end > > end > > > > The above is just a recreation of what I am looking to do. So > > basically I want to be able to access objects like the connector from > > the child, or even the parent itself by something like > > self.parent.attributes. > > > > Any ideas? > > > > thanks, > > > > nayeem > > > > > > > > >-- cashflowclublondon.co.uk ("`-''''-/").___..--''''"`-._ `6_ 6 ) `-. ( ).`-.__.`) (_Y_.)'' ._ ) `._ `. ``-..-'' _..`--''_..-_/ /--''_.'' ,'' (il),-'''' (li),'' ((!.-'' . --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
developerinlondon wrote:> Hi, > > I am having difficulties accessing parent properties/attributes from > the child. Anyone could shed some lights onto this? > > Heres a sample code:[...] Your sample code will not do the job. All you need is class Parent > ActiveRecord::Base has_many :children end class Child > ActiveRecord::Base belongs_to :parent end ...and parent.children and child.parent will work as you''d like. @connector is unnecessary. Check the docs for Associations; Rails makes this ridiculously simple. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
That Works! Thanks! 2009/6/7 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > developerinlondon wrote: > > Hi, > > > > I am having difficulties accessing parent properties/attributes from > > the child. Anyone could shed some lights onto this? > > > > Heres a sample code: > [...] > > Your sample code will not do the job. All you need is > > class Parent > ActiveRecord::Base > has_many :children > end > > class Child > ActiveRecord::Base > belongs_to :parent > end > > ...and parent.children and child.parent will work as you''d like. > @connector is unnecessary. > > Check the docs for Associations; Rails makes this ridiculously simple. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >-- cashflowclublondon.co.uk ("`-''''-/").___..--''''"`-._ `6_ 6 ) `-. ( ).`-.__.`) (_Y_.)'' ._ ) `._ `. ``-..-'' _..`--''_..-_/ /--''_.'' ,'' (il),-'''' (li),'' ((!.-'' . --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---