Manuel Holtgrewe
2005-Sep-15 20:10 UTC
Auto-Save on n:m Relation Prevents Preview Function?
Hi I want to implement the following: I have an Article ActiveRecord with title and body. I also have an Author ActiveRecord with a "name" property. There is an n:m relations between Author and Article. I have an ArticleController that implements CRUD for Article datasets. This controller has the "classic" actions: new, create, edit, update, delete, destroy, list. I want to be able to post to "create", with a "Preview" button that will then preview the changes made to the Article. The Article edit form also includes a list you can select the authors of this Article from. For the title and body, this is no problem since these fields are only updated in the database when I call @article.save. However, for n:m relations, this is different; the changes are reflected to the database immediately (also see AR documentation at http://tinyurl.com/ aujwb). Is there an elegant way to do this preview or will I have to store things in the session and push them into another template variable. I''d like if it not comes to this since I hoped to do things consistently with an DB abstraction layer like ActiveRecord. Regards Manuel Holtgrewe
Mark Reginald James
2005-Sep-17 20:28 UTC
Re: Auto-Save on n:m Relation Prevents Preview Function?
Manuel Holtgrewe wrote:> I have an Article ActiveRecord with title and body. I also have an > Author ActiveRecord with a "name" property. There is an n:m relations > between Author and Article. > > I have an ArticleController that implements CRUD for Article datasets. > This controller has the "classic" actions: new, create, edit, update, > delete, destroy, list. I want to be able to post to "create", with a > "Preview" button that will then preview the changes made to the > Article. The Article edit form also includes a list you can select the > authors of this Article from. > > For the title and body, this is no problem since these fields are only > updated in the database when I call @article.save. However, for n:m > relations, this is different; the changes are reflected to the database > immediately (also see AR documentation at http://tinyurl.com/ aujwb). > > Is there an elegant way to do this preview or will I have to store > things in the session and push them into another template variable. I''d > like if it not comes to this since I hoped to do things consistently > with an DB abstraction layer like ActiveRecord.As suggested on p.232 of the Agile book, how about turning the join table into an "Authorship" model, and the HABTM relations into HM relations, on which you can use the build method to create the structure without auto-save? -- We develop, watch us RoR, in numbers too big to ignore.
Manuel Holtgrewe
2005-Sep-17 20:45 UTC
Re: Re: Auto-Save on n:m Relation Prevents Preview Function?
Am 17.09.2005 um 22:28 schrieb Mark Reginald James:> As suggested on p.232 of the Agile book, how about turning the join > table > into an "Authorship" model, and the HABTM relations into HM > relations, on > which you can use the build method to create the structure without > auto-save?Thanks for your interest in my problem. However, this does not help when you want to remove elements from a relation. There seems to be no equivalent of build for removing elements from a collection. Regards Manuel Holtgrewe
Mark Reginald James
2005-Sep-17 21:18 UTC
Re: Auto-Save on n:m Relation Prevents Preview Function?
Manuel Holtgrewe wrote:> > Am 17.09.2005 um 22:28 schrieb Mark Reginald James: > >> As suggested on p.232 of the Agile book, how about turning the join >> table >> into an "Authorship" model, and the HABTM relations into HM >> relations, on >> which you can use the build method to create the structure without >> auto-save? > > Thanks for your interest in my problem. However, this does not help > when you want to remove elements from a relation. There seems to be no > equivalent of build for removing elements from a collection.Looking at the AR code you could use association.to_ary.delete(model_instance) to get the right association object array for your preview views. -- We develop, watch us RoR, in numbers too big to ignore.
Manuel Holtgrewe
2005-Sep-17 22:25 UTC
Re: Re: Auto-Save on n:m Relation Prevents Preview Function?
Am 17.09.2005 um 23:18 schrieb Mark Reginald James:> Looking at the AR code you could use association.to_ary.delete > (model_instance) > to get the right association object array for your preview views.If I understand you right, I''d assing the value of the changed array in another template variable then? This is not really optimal in my opinion. This seems to be equal to do something like "@authors = model.authors". Additionally, I don''t get model validation that way. Regards Manuel Holtgrewe
Julian ''Julik'' Tarkhanov
2005-Sep-18 00:12 UTC
Re: Auto-Save on n:m Relation Prevents Preview Function?
On 15-sep-2005, at 22:10, Manuel Holtgrewe wrote:> > Is there an elegant way to do this preview or will I have to store > things in the session and push them into another template variable. > I''d like if it not comes to this since I hoped to do things > consistently with an DB abstraction layer like ActiveRecord.You will. As an option just store the whole record in the session when editing. -- Julian "Julik" Tarkhanov
Mark Reginald James
2005-Sep-18 00:19 UTC
Re: Auto-Save on n:m Relation Prevents Preview Function?
Manuel Holtgrewe wrote:> > Am 17.09.2005 um 23:18 schrieb Mark Reginald James: > >> Looking at the AR code you could use association.to_ary.delete >> (model_instance) >> to get the right association object array for your preview views. > > > If I understand you right, I''d assing the value of the changed array in > another template variable then? This is not really optimal in my > opinion. This seems to be equal to do something like "@authors = > model.authors". Additionally, I don''t get model validation that way.No the real association array variable is used, but it doesn''t mess with the DB. Like: class Article has_many :authors def add_author(author, preview=false) if preview self.authors.to_ary << author else self.authors << author end end def del_author(author, preview=false) if preview self.authors.to_ary.delete(author) else self.authors.delete(author) end end end class ArticleController private def build_article(params, preview) @article = ... @article.add_author( Author.new(params[''Author1'']), preview ) @article.del_author( Author.find(34), preview ) end public def show_preview build_article(params,true) if @article.valid? ... end def show_final build_article(params,false) if @article.save ... end end Then in both views either use @article.authors, which is cached, or set @authors = @article.authors at the end of build_article. Should work with both HABTM and HM. -- We develop, watch us RoR, in numbers too big to ignore.
Manuel Holtgrewe
2005-Sep-18 08:34 UTC
Re: Auto-Save on n:m Relation Prevents Preview Function?
Am 18.09.2005 um 02:12 schrieb Julian ''Julik'' Tarkhanov:> > On 15-sep-2005, at 22:10, Manuel Holtgrewe wrote: > >> >> Is there an elegant way to do this preview or will I have to store >> things in the session and push them into another template >> variable. I''d like if it not comes to this since I hoped to do >> things consistently with an DB abstraction layer like ActiveRecord. >> > > You will. As an option just store the whole record in the session > when editing.Then I lose validation through the ActiveRecord model. Regards Manuel Holtgrewe