Hi Maybe you remember my questions related to previews and the "eager updating" of associations with ActiveRecord. Since I have not yet come up with or been suggested a workaround that I feel sufficient to please my longing for elegant and clean code I have started to consider changing ActiveRecord to support for relations with "lazy updates". "Lazy updates" is what I call changes to a relation are only persisted when the owning record''s "save" method is executed. I have dug through ActiveRecord''s association code over the last days and come up with the following proposal: http://dev.rubyonrails.com/ticket/2238 Could you please comment on whether you find this useful and whether you agree with the points raised and the proposals made? It''s quite lengthy but should describe the major points that would be required to add ":lazy_update" to ActiveRecord and make some things more consistent. I would be willing to do the changes to the code since I need the preview feature I earlier wrote about on this list. However, changing something so central to ActiveRecord and using it only as a patch to my "vendor rails" would basically make it a fork and incompatible with RoR. This would make it basically useless to me, then, too. Regards Manuel Holtgrewe
Hi, having read your proposal I can say: I''d like to see this enhancement for ActiveRecord. The inconsistency you are describing caused major confusion for me when I first started learning RoR. I ended up using a poorly implemented version of your second "implementation idea" (habtm..) but I''m not happy with it. Thanks for your work on this idea, Lars
Manuel Holtgrewe wrote:> I have dug through ActiveRecord''s association code over the last days > and come up with the following proposal: > > http://dev.rubyonrails.com/ticket/2238 > > Could you please comment on whether you find this useful and whether > you agree with the points raised and the proposals made? It''s quite > lengthy but should describe the major points that would be required to > add ":lazy_update" to ActiveRecord and make some things more consistent.Indeed, I think it makes relations handling by AR more consistent. You mention that you need it to implement a Preview. Could you please show an example in pseudo-code on how this lazy relation saving would help to build a preview ? would you keep unsaved-to-db AR objects into session before deciding to save them to db if user approves ? I think your proposal would be easier to understand with an example of code it''ll allow. -- Jean-Christophe Michel
Am 22.09.2005 um 23:36 schrieb Jean-Christophe Michel:> Indeed, I think it makes relations handling by AR more consistent. > You mention that you need it to implement a Preview. Could you please > show an example in pseudo-code on how this lazy relation saving would > help to build a preview ? would you keep unsaved-to-db AR objects into > session before deciding to save them to db if user approves ? > I think your proposal would be easier to understand with an example of > code it''ll allow.OK, here we go (major points are marked with #!!): ==8<======================================================== def update @article = Article.find(params[:id]) # bulk assign simple ("atomic") attributes @article.attributes = params[:article] # get an array of authors params[:article][:authors] = [] if params[:article][:authors].nil? roles = params[:article][:authors].collect { |i| Author.find(i) } # !! # The following line would not work for previews with current code # since it would change the database state! However, with the # proposed patch it does not change the database''s state. # !! @article.authors = roles if !params[:submit][:update].nil? # update mode if @user.save # !! # If @user.save has been successful, both attributes and # relations have been stored to the database. # !! flash[:notice] = ''User was successfully updated.'' redirect_to :action => ''show'', :id => @user.to_param else render :action => ''edit'' end else # preview mode render :action => :edit end rescue ActiveRecord::RecordNotFound flash[:error] = ''You sent an invalid request.'' redirect_to :action => ''list'' end ==8<======================================================== Notice that all is pretty "natural", i.e. you can simply update attributes and relations of the object without changing the database state at all. Only when you call "model.save", the changes are preserved to the database. With the current code, you cannot do "model.association = foo" for previews because this would change the database state. Regards Manuel Holtgrewe