Greg Hauptmann
2009-Jan-15 01:53 UTC
Does Rails have a mode which ensures associations work at the object level (i.e. prior to any DB saves)??? Is there a way to do this?
Hi, I''m noted with Rails that if one assigns one object (say Book "b") to another object (say Chapter "c") that the call "b.chapters" doesn''t work. Question: Is there a way to ensures associations work both ways at the object level (i.e. prior to any DB saves)? (i.e. so in the above cases after I allocate a Book against a Chapter (e.g. c.book = b), that "b.chapters" should then work? Overall example: b = Book.new c = Chapter.new c.book = b c.book ==> works and gives b object b.chapters ==> DOES NOT WORK - gives [] Also: b = Book.new c = Chapter.new b.chapters = [c] b.chapters ==> works c.book ==> DOES NOT WORK Notes: * This is a specific question I have (relates to what I''m trying to achieve is a separate post "Validation spanning multiple models(tables) - how can this "): Thanks in advance -- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Jan-15 01:58 UTC
Re: Does Rails have a mode which ensures associations work at the object level (i.e. prior to any DB saves)??? Is there a way to do this?
There doesn''t seem to be and from the object level doing: book.chapters << chapter Doesn''t mean that chapter.book will be assigned, as you haven''t assigned it yourself. In most programming languages there is no way to make a two-way association automatically. Even well know ORM tools like Hibernate don''t do this, as it''s way too intrusive. But you can always override rails default behaviour. - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Wed, Jan 14, 2009 at 10:53 PM, Greg Hauptmann <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m noted with Rails that if one assigns one object (say Book "b") to > another object (say Chapter "c") that the call "b.chapters" doesn''t work. > > Question: Is there a way to ensures associations work both ways at the > object level (i.e. prior to any DB saves)? (i.e. so in the above cases > after I allocate a Book against a Chapter (e.g. c.book = b), that > "b.chapters" should then work? > > Overall example: > b = Book.new > c = Chapter.new > c.book = b > c.book ==> works and gives b object > b.chapters ==> DOES NOT WORK - gives [] > > Also: > b = Book.new > c = Chapter.new > b.chapters = [c] > b.chapters ==> works > c.book ==> DOES NOT WORK > > Notes: > * This is a specific question I have (relates to what I''m trying to achieve > is a separate post "Validation spanning multiple models(tables) - how can > this "): > > Thanks in advance > > -- > Greg > http://blog.gregnet.org/ > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2009-Jan-15 02:23 UTC
Re: Does Rails have a mode which ensures associations work at the object level (i.e. prior to any DB saves)??? Is there a way to do this?
any pointers/ideas re how to over-ride the Rails behavior for some classes? I had a quick look at how an association value is assigned & it looked a bit hairy (all this reflection stuff)...wondering if it maybe non-trivial & have gottchas? tks On 1/15/09, Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There doesn''t seem to be and from the object level doing: > > book.chapters << chapter > > Doesn''t mean that chapter.book will be assigned, as you haven''t > assigned it yourself. In most programming languages there is no way to > make a two-way association automatically. Even well know ORM tools > like Hibernate don''t do this, as it''s way too intrusive. > > But you can always override rails default behaviour. > > - > Maurício Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) > > > > On Wed, Jan 14, 2009 at 10:53 PM, Greg Hauptmann > <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> >> I''m noted with Rails that if one assigns one object (say Book "b") to >> another object (say Chapter "c") that the call "b.chapters" doesn''t work. >> >> Question: Is there a way to ensures associations work both ways at the >> object level (i.e. prior to any DB saves)? (i.e. so in the above cases >> after I allocate a Book against a Chapter (e.g. c.book = b), that >> "b.chapters" should then work? >> >> Overall example: >> b = Book.new >> c = Chapter.new >> c.book = b >> c.book ==> works and gives b object >> b.chapters ==> DOES NOT WORK - gives [] >> >> Also: >> b = Book.new >> c = Chapter.new >> b.chapters = [c] >> b.chapters ==> works >> c.book ==> DOES NOT WORK >> >> Notes: >> * This is a specific question I have (relates to what I''m trying to >> achieve >> is a separate post "Validation spanning multiple models(tables) - how can >> this "): >> >> Thanks in advance >> >> -- >> Greg >> http://blog.gregnet.org/ >> >> >> >> > >> > > > >-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-15 11:35 UTC
Re: Does Rails have a mode which ensures associations work at the object level (i.e. prior to any DB saves)??? Is there a way to do this?
On 15 Jan 2009, at 02:23, Greg Hauptmann wrote:> > any pointers/ideas re how to over-ride the Rails behavior for some > classes? > > I had a quick look at how an association value is assigned & it looked > a bit hairy (all this reflection stuff)...wondering if it maybe > non-trivial & have gottchas? >There is a plugin ( http://github.com/h-lame/parental_control/tree/master ) which implements a solution to this. There is a proposal to integrate it (or a variant of it) into core. Fred> tks > > On 1/15/09, Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> There doesn''t seem to be and from the object level doing: >> >> book.chapters << chapter >> >> Doesn''t mean that chapter.book will be assigned, as you haven''t >> assigned it yourself. In most programming languages there is no way >> to >> make a two-way association automatically. Even well know ORM tools >> like Hibernate don''t do this, as it''s way too intrusive. >> >> But you can always override rails default behaviour. >> >> - >> Maurício Linhares >> http://alinhavado.wordpress.com/ (pt-br) | http:// >> blog.codevader.com/ (en) >> >> >> >> On Wed, Jan 14, 2009 at 10:53 PM, Greg Hauptmann >> <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Hi, >>> >>> I''m noted with Rails that if one assigns one object (say Book "b") >>> to >>> another object (say Chapter "c") that the call "b.chapters" >>> doesn''t work. >>> >>> Question: Is there a way to ensures associations work both ways at >>> the >>> object level (i.e. prior to any DB saves)? (i.e. so in the above >>> cases >>> after I allocate a Book against a Chapter (e.g. c.book = b), that >>> "b.chapters" should then work? >>> >>> Overall example: >>> b = Book.new >>> c = Chapter.new >>> c.book = b >>> c.book ==> works and gives b object >>> b.chapters ==> DOES NOT WORK - gives [] >>> >>> Also: >>> b = Book.new >>> c = Chapter.new >>> b.chapters = [c] >>> b.chapters ==> works >>> c.book ==> DOES NOT WORK >>> >>> Notes: >>> * This is a specific question I have (relates to what I''m trying to >>> achieve >>> is a separate post "Validation spanning multiple models(tables) - >>> how can >>> this "): >>> >>> Thanks in advance >>> >>> -- >>> Greg >>> http://blog.gregnet.org/ >>> >>> >>> >>>> >>> >> >>> >> > > > -- > Greg > http://blog.gregnet.org/ > > --~--~---------~--~----~------------~-------~--~----~ > 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 > -~----------~----~----~----~------~----~------~--~--- >