Hi, I''m using the following controller code to either instantiate a new Diary object or retrieve one for editing in an add/edit form. I''m having a problem with the diary_images belonging to the diary (a has_many relationship). The form in the view should always present 3 file upload slots for the images, so I''m using the simple loop to supplement any diary_images the diary already has. For the Diary.new, this works as expected, with the empty upload slots appearing in the view. With the Diary.find version however, the code is adding the extra diary_images as empty records in the database, with the diary foreign key. I would have expected this to only happen if I called save on the Diary model. Am I doing something wrong, or is there another way for me to go about it? Again, thanks for any help. Matt Controller code: def edit if request.get? if params[:id] @diary = Diary.find(params[:id]) else @diary = Diary.new end count = @image_max - @diary.diary_images.size count.times do |i| @diary.diary_images << DiaryImage.new end end end
Well, the code you''ve shown should not create or save anything (I''m a little unsure as to why you have the check for the request being a get though). I''m assuming that the save is happening when the edit form is then submitted - in which case I''d fully expect a retrieved diary model to be saved with empty diary_images unless you explicitly ignore empty diary_images in the controller method which handles the actual saving (assuming the default scaffolding, I''d say check the code in update). I''d say your best bet is to tail the logs and watch what SQL gets run when - perhaps even put some logger.debug calls in each of the controllers methods so you can ge a better feel for what methods are getting called when. On 6/28/05, Matt <listaction-FXsoPwVLZSKdyyGsB3R/UQ@public.gmane.org> wrote:> Hi, > > I''m using the following controller code to either instantiate a new > Diary object or retrieve one for editing in an add/edit form. > > I''m having a problem with the diary_images belonging to the diary (a > has_many relationship). The form in the view should always present 3 > file upload slots for the images, so I''m using the simple loop to > supplement any diary_images the diary already has. > > For the Diary.new, this works as expected, with the empty upload > slots appearing in the view. With the Diary.find version however, > the code is adding the extra diary_images as empty records in the > database, with the diary foreign key. I would have expected this to > only happen if I called save on the Diary model. Am I doing > something wrong, or is there another way for me to go about it? > > Again, thanks for any help. > > Matt > > > Controller code: > > def edit > > if request.get? > if params[:id] > @diary = Diary.find(params[:id]) > else > @diary = Diary.new > end > > count = @image_max - @diary.diary_images.size > count.times do |i| > @diary.diary_images << DiaryImage.new > end > > end > > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
Thanks for the reply. I think I should have been clearer with my example. The check for the request.get? is because this code should be run when the edit form is created and displayed, rather than submitted (via post). I''m not actually submitting the form before the empty diary_images are added to the DB. I''ll follow your suggestion and add in the logger.debug calls to the controller and model. Thanks for the help. Matt On 28 Jun 2005, at 10:12, Sam Newman wrote:> Well, the code you''ve shown should not create or save anything (I''m a > little unsure as to why you have the check for the request being a get > though). I''m assuming that the save is happening when the edit form is > then submitted - in which case I''d fully expect a retrieved diary > model to be saved with empty diary_images unless you explicitly ignore > empty diary_images in the controller method which handles the actual > saving (assuming the default scaffolding, I''d say check the code in > update). I''d say your best bet is to tail the logs and watch what SQL > gets run when - perhaps even put some logger.debug calls in each of > the controllers methods so you can ge a better feel for what methods > are getting called when. > > On 6/28/05, Matt <listaction-FXsoPwVLZSKdyyGsB3R/UQ@public.gmane.org> wrote: > >> Hi, >> >> I''m using the following controller code to either instantiate a new >> Diary object or retrieve one for editing in an add/edit form. >> >> I''m having a problem with the diary_images belonging to the diary (a >> has_many relationship). The form in the view should always present 3 >> file upload slots for the images, so I''m using the simple loop to >> supplement any diary_images the diary already has. >> >> For the Diary.new, this works as expected, with the empty upload >> slots appearing in the view. With the Diary.find version however, >> the code is adding the extra diary_images as empty records in the >> database, with the diary foreign key. I would have expected this to >> only happen if I called save on the Diary model. Am I doing >> something wrong, or is there another way for me to go about it? >> >> Again, thanks for any help. >> >> Matt >> >> >> Controller code: >> >> def edit >> >> if request.get? >> if params[:id] >> @diary = Diary.find(params[:id]) >> else >> @diary = Diary.new >> end >> >> count = @image_max - @diary.diary_images.size >> count.times do |i| >> @diary.diary_images << DiaryImage.new >> end >> >> end >> >> end >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > sam > http://www.magpiebrain.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Having the same method for both creation of the form and handling the submit is going to lead to longer, more complex method - I''d suggest having completely seperate methods as per the scaffolding (where edit sets up the state of the form, and update handles the post)t -that way you''ll have smaller, more focused, more maintanable methods. If your edit method is coping with both the initialisation of the form and the posting of the form, then where in your code examples are the save or update_attributes calls? On 6/28/05, Matt <listaction-FXsoPwVLZSKdyyGsB3R/UQ@public.gmane.org> wrote:> Thanks for the reply. > I think I should have been clearer with my example. The check for the > request.get? is because this code should be run when the edit form is > created and displayed, rather than submitted (via post). I''m not > actually submitting the form before the empty diary_images are added > to the DB. > I''ll follow your suggestion and add in the logger.debug calls to the > controller and model. > Thanks for the help. > > Matt-- sam http://www.magpiebrain.com/
I hadn''t inserted the save calls yet, was still trying to iron out the initialisation! I am going to follow your advice however and separate the functionality according to the scaffolding. Regarding the issue I was having, it seems it was simply a case of not having enough validation on the DiaryImage model, allowing it to save empty records into the DB. I still wouldn''t have expected it to save without explicitly calling any save methods, but the validation has fixed the issue for me. Thanks again. On 28 Jun 2005, at 10:33, Sam Newman wrote:> Having the same method for both creation of the form and handling the > submit is going to lead to longer, more complex method - I''d suggest > having completely seperate methods as per the scaffolding (where edit > sets up the state of the form, and update handles the post)t -that way > you''ll have smaller, more focused, more maintanable methods. If your > edit method is coping with both the initialisation of the form and the > posting of the form, then where in your code examples are the save or > update_attributes calls? > > On 6/28/05, Matt <listaction-FXsoPwVLZSKdyyGsB3R/UQ@public.gmane.org> wrote: > >> Thanks for the reply. >> I think I should have been clearer with my example. The check for the >> request.get? is because this code should be run when the edit form is >> created and displayed, rather than submitted (via post). I''m not >> actually submitting the form before the empty diary_images are added >> to the DB. >> I''ll follow your suggestion and add in the logger.debug calls to the >> controller and model. >> Thanks for the help. >> >> Matt >> > > -- > sam > http://www.magpiebrain.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 28-jun-2005, at 10:14, Matt wrote:> Hi, > > I''m using the following controller code to either instantiate a new > Diary object or retrieve one for editing in an add/edit form. > > I''m having a problem with the diary_images belonging to the diary > (a has_many relationship). The form in the view should always > present 3 file upload slots for the images, so I''m using the simple > loop to supplement any diary_images the diary already has. > > For the Diary.new, this works as expected, with the empty upload > slots appearing in the view. With the Diary.find version however, > the code is adding the extra diary_images as empty records in the > database, with the diary foreign key. I would have expected this to > only happen if I called save on the Diary model. Am I doing > something wrong, or is there another way for me to go about it?Matt, this is unbelievable how tasks match - I am banging my head against the same kind of problem but in my case it''s aggravated by the fact that I got to have 2 file uploads per slot :-) -- Julian "Julik" Tarkhanov