I have three model objects: 1. Document 2. Section 3. Content According to my requirements a Document must have at least one Section. Similarly a Section must have at least one Content. How can I add a Section when a Document is created and add a Content when a Section is created? My code: class Document < ActiveRecord::Base has_many :sections end class Section < ActiveRecord::Base belongs_to :document has_many :contents end class Content < ActiveRecord::Base belongs_to :section end I have tried several approaches. One approach I used was to add an after_initialize callback to Document and Section which called a method that added the appropriate object to the parent object. Thank you for your time and insight.
On 8/25/05, Kesslers <kesslerdn-v+z9hhnkyldeoWH0uzbU5w@public.gmane.org> wrote:> I have three model objects: > > 1. Document > 2. Section > 3. Content > > According to my requirements a Document must have at least one Section. > Similarly a Section must have at least one Content. > > How can I add a Section when a Document is created and add a Content when a > Section is created? > > My code: > > class Document < ActiveRecord::Base > has_many :sections > end > > class Section < ActiveRecord::Base > belongs_to :document > has_many :contents > end > > class Content < ActiveRecord::Base > belongs_to :section > end > > I have tried several approaches. One approach I used was to add an > after_initialize callback to Document and Section which called a method that > added the appropriate object to the parent object. > > Thank you for your time and insight.Use an after_save. This way when you create the new object, you can associate it with the current one immediately since it will have an ID: I have a very similar example: http://collaboa.techno-weenie.net/repository/file/supasite/components/supasite/section.rb -- rick http://techno-weenie.net
Alright, I guess I missed a few points in my first message. I have a screen where a user can enter a new document. By default I can make the screen have one set of document fields with one set of section fields and one set of content fields. However, I want the user to be able to add new sections to the document and new content to a given section. In order for the user to be able to cancel before they save a document they must be able to add these sections and content into an object that is NOT saved to the database. I have attempted to use an object in the session called Draft. I am having difficulty not creating another model object for document, section and content which would break the DRY principle. When I begin to store info in the model object Draft (that I store in the session) I encounter the same issues. In order to know which section to add content I must add sections to a document and reference each secion by an id. Please help. On 8/25/05, Kesslers <kesslerdn-v+z9hhnkyldeoWH0uzbU5w@public.gmane.org> wrote:> I have three model objects: > > 1. Document > 2. Section > 3. Content > > According to my requirements a Document must have at least one Section. > Similarly a Section must have at least one Content. > > How can I add a Section when a Document is created and add a Content whena> Section is created? > > My code: > > class Document < ActiveRecord::Base > has_many :sections > end > > class Section < ActiveRecord::Base > belongs_to :document > has_many :contents > end > > class Content < ActiveRecord::Base > belongs_to :section > end > > I have tried several approaches. One approach I used was to add an > after_initialize callback to Document and Section which called a methodthat> added the appropriate object to the parent object. > > Thank you for your time and insight.Use an after_save. This way when you create the new object, you can associate it with the current one immediately since it will have an ID: I have a very similar example: http://collaboa.techno-weenie.net/repository/file/supasite/components/supasi te/section.rb -- rick http://techno-weenie.net
Kesslers wrote:> Alright, I guess I missed a few points in my first message. I have a screen > where a user can enter a new document. By default I can make the screen > have one set of document fields with one set of section fields and one set > of content fields. However, I want the user to be able to add new sections > to the document and new content to a given section. In order for the user > to be able to cancel before they save a document they must be able to add > these sections and content into an object that is NOT saved to the database.I''d make a distinction between ''document saved for user'' and ''object saved to db''. I''d always save objects to db and add a field ''saved_at'' DATE containing the last user save action. -- Jean-Christophe Michel