I''m not sure if I''m using AR correctly or not. I''d like to create some objects that map to the db, but persist them only after a particular number of steps are completed. Right now, on the first step, I run the create method, and that just creates an empty record in the db. So if the user doesn''t complete all the steps, I''m left with an empty record, which is obviously ugly and adds up over time. Am I not using AR and the model the way I should be? Is there any way I can prevent all these empty records in the db?
On 5/3/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m not sure if I''m using AR correctly or not. I''d like to create > some objects that map to the db, but persist them only after a > particular number of steps are completed. Right now, on the first > step, I run the create method, and that just creates an empty record > in the db. So if the user doesn''t complete all the steps, I''m left > with an empty record, which is obviously ugly and adds up over time. > Am I not using AR and the model the way I should be? Is there any way > I can prevent all these empty records in the db? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Read the ActiveRecord docs for #create [1] That method explicitly calls save on the record. What you want is to use #new, which does not automatically save the record. Then you can call #save on the record once you''ve gotten through the various steps. HTH, Jason [1] http://ar.rubyonrails.com
The item that''s being created has a has_many relation, and each object below that has another has_many. If I just do #new, it creates a record for the top-level object when I #save at the end, but not the objects below it in the relation. I''ve tried doing has_many :options, :dependent => true But that doesn''t do anything. These steps occur across multiple pages, so all the objects aren''t being created and saved immediately. Is it possible to make it work like that? Or would I have to save all that info to the session? That''s an ugly hack, but if that''s what I have to do... On 5/3/05, Jason Foreman <threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/3/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m not sure if I''m using AR correctly or not. I''d like to create > > some objects that map to the db, but persist them only after a > > particular number of steps are completed. Right now, on the first > > step, I run the create method, and that just creates an empty record > > in the db. So if the user doesn''t complete all the steps, I''m left > > with an empty record, which is obviously ugly and adds up over time. > > Am I not using AR and the model the way I should be? Is there any way > > I can prevent all these empty records in the db? > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > Read the ActiveRecord docs for #create [1] That method explicitly > calls save on the record. What you want is to use #new, which does > not automatically save the record. Then you can call #save on the > record once you''ve gotten through the various steps. > > HTH, > > Jason > > [1] http://ar.rubyonrails.com >
Hi Pat, On 4.5.2005, at 01:50, Pat Maddox wrote:> The item that''s being created has a has_many relation, and each object > below that has another has_many. If I just do #new, it creates a > record for the top-level object when I #save at the end, but not the > objects below it in the relation. I''ve tried doing > has_many :options, :dependent => true > > But that doesn''t do anything.It has nothing to do with this. It means that the related options will be deleted too when the current object is deleted.> These steps occur across multiple > pages, so all the objects aren''t being created and saved immediately. > Is it possible to make it work like that? Or would I have to save all > that info to the session? That''s an ugly hack, but if that''s what I > have to do...Well, you have to pass the information somehow. As the individual steps are separate requests, there''s nothing persistent between them. So storing the info in the session is one way to do it. Another would be to create (and save) the objects on the fly, and when the final step is done you can set a special field like "complete" to 1. Then you can make a sweeper that periodically cleans up incomplete fields from the database. //jarkko> > > > On 5/3/05, Jason Foreman <threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 5/3/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> I''m not sure if I''m using AR correctly or not. I''d like to create >>> some objects that map to the db, but persist them only after a >>> particular number of steps are completed. Right now, on the first >>> step, I run the create method, and that just creates an empty record >>> in the db. So if the user doesn''t complete all the steps, I''m left >>> with an empty record, which is obviously ugly and adds up over time. >>> Am I not using AR and the model the way I should be? Is there any >>> way >>> I can prevent all these empty records in the db? >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> Read the ActiveRecord docs for #create [1] That method explicitly >> calls save on the record. What you want is to use #new, which does >> not automatically save the record. Then you can call #save on the >> record once you''ve gotten through the various steps. >> >> HTH, >> >> Jason >> >> [1] http://ar.rubyonrails.com >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails