Lol don''t ask why but I would like to use to code I found on the forum
to copy two values from table A to table B. It looks a bit roundabout
but it doesn''t matter to me.
Okay so I know that this part goes in the controller class(i dont think
I have to modify it at all?):
def copy_attributes(dest, src, *attrs)
for attr in attrs
dest.send(attr.to_s + ''='',src.send(attr))
end
end
Then where does this line of code go/how do I execute the copy? Would I
include it in def new, or between the "ends" in the above code, or
wrap
it in <=% %> tags in the view.
copy_attributes(bidule, biniou, :foo, :bar, :baz)
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hey I just decided to start tackling this one again. Can anybody point me in the right direction? Sarah Jackson wrote:> Lol don''t ask why but I would like to use to code I found on the forum > to copy two values from table A to table B. It looks a bit roundabout > but it doesn''t matter to me. > > > Okay so I know that this part goes in the controller class(i dont think > I have to modify it at all?): > > def copy_attributes(dest, src, *attrs) > for attr in attrs > dest.send(attr.to_s + ''='',src.send(attr)) > end > end > > > Then where does this line of code go/how do I execute the copy? Would I > include it in def new, or between the "ends" in the above code, or wrap > it in <=% %> tags in the view. > > copy_attributes(bidule, biniou, :foo, :bar, :baz)-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On May 4, 2007, at 12:57 PM, Sarah Jackson wrote:> Hey I just decided to start tackling this one again. Can anybody > point me in the right direction? > > Sarah Jackson wrote: >> Lol don''t ask why but I would like to use to code I found on the >> forum >> to copy two values from table A to table B. It looks a bit >> roundabout >> but it doesn''t matter to me. >> >> >> Okay so I know that this part goes in the controller class(i dont >> think >> I have to modify it at all?): >> >> def copy_attributes(dest, src, *attrs) >> for attr in attrs >> dest.send(attr.to_s + ''='',src.send(attr)) >> end >> end >> >> >> Then where does this line of code go/how do I execute the copy? >> Would I >> include it in def new, or between the "ends" in the above code, or >> wrap >> it in <=% %> tags in the view. >> >> copy_attributes(bidule, biniou, :foo, :bar, :baz)You definitely shouldn''t be changing models in the view. Depending on what you''re trying to do, you might be better off putting your attribute copying code in your model, along the lines of: class ModelA < ActiveRecord::Base end class ModelB < ActiveRecord::Base def self.create_from_a(model_a) # Set this model''s attributes from other model''s attributes save end end Then in your controller you can call: class MyController < Application def my_action @my_a = ModelA.find(params[:id]) @my_b = ModelB.create_from_a(@my_a) end end James. -- James Stewart http://jystewart,net/process/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---