Richard Sandilands
2005-Jun-22 02:36 UTC
Rewriting action method as a class method (newbie)
Hi there I have an action method in a controller that serves to duplicate an object and *some* of it''s child objects. A Survey has many values and many participants. I am cloning a survey and its values but not participants. That''s the only association that I want to clone along with the parent; the newly cloned survey will have no participants. Being the newcomer to ruby and rails that I am, I wrote this as an action method: def duplicate @survey = Survey.find(@params[:id]) @values = Value.find(:all, :conditions => "survey_id = #{@survey.id}") @survey_clone = @survey.clone @survey_clone.survey_name = "#{@survey.survey_name} clone" if @values and @survey_clone.save -j2iHPqhclm1bj60gHklGcw@public.gmane.org do |value| @value_clone = value.clone @value_clone.survey_id = @survey_clone.id end flash[:notice] = "Survey successfully cloned" redirect_to :action => ''edit'', :id=> @survey_clone.id end end It works. I want to rewrite this now in the Model class in survey.rb but am going around in syntactic circles. I''m stuck on how much of the above stays in the controller and how much in the new class method. Any clues much appreciated.
Michael Koziarski
2005-Jun-22 02:50 UTC
Re: Rewriting action method as a class method (newbie)
On 6/22/05, Richard Sandilands <infoarts-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there > > I have an action method in a controller that serves to duplicate an > object and *some* of it''s child objects. > > A Survey has many values and many participants. I am cloning a survey > and its values but not participants. That''s the only association that > I want to clone along with the parent; the newly cloned survey will > have no participants. > > Being the newcomer to ruby and rails that I am, I wrote this as an > action method: > > def duplicate > @survey = Survey.find(@params[:id]) > @values = Value.find(:all, :conditions => "survey_id = #{@survey.id}") > @survey_clone = @survey.clone > @survey_clone.survey_name = "#{@survey.survey_name} clone" > if @values and @survey_clone.save > @values.each do |value| > @value_clone = value.clone > @value_clone.survey_id = @survey_clone.id > end > flash[:notice] = "Survey successfully cloned" > redirect_to :action => ''edit'', :id=> @survey_clone.id > end > end > > It works. I want to rewrite this now in the Model class in survey.rb > but am going around in syntactic circles. I''m stuck on how much of the > above stays in the controller and how much in the new class method. > Any clues much appreciated.Is there a name that your business users use to describe this process? Copying a Survey? Duplicating? Use that name, I''ve stuck with duplicate class Survey < ActiveRecord::Base def duplicate new_survey = Survey.new(...) # set your survey level attributes here new_survey.values << self.values.collect {|value| value.clone} # add cloned values return new_survey end end That should work> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz