is it possible to dynamically create controller code?? for example if you have a parent element @parent and children element @parent.daughter and @parent.son. @parent.daughter.class = daughter @parent.son.class = son so if we wanted to add a dog to all of our sons or all of our daughters when a controller element is called we would have to do something like this. def dog if @child.class == son for son in @parent.sons son.dog = true son.save! end else for daughter in @parent.daughters daughter.dog = true daughter.save! end end end Duplicating code like this is annoying and not very DRY is anything like this remotely possible instead ?? def dog child_type = @child.class for child_type in @parent.child_type+s child_type.dog = true child_type.save! end end this would cut code almost in half, for this situation. If there is anything capable of dynamically generating controller code based on object classes or even defined variables please let me know!! -- 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 Jun 16, 8:17 am, Richard Schneeman <rails-mailing-l...@andreas- s.net> wrote:> > Duplicating code like this is annoying and not very DRY is anything like > this remotely possible instead ?? > > def dog > child_type = @child.class > for child_type in @parent.child_type+s > child_type.dog = true > child_type.save! > end > end >Well you could write something like this: def set_dog(association_name) collection = @parent.send association_name collection.each {|element| element.update_attributes :dog => true} end Although I''d have this as method on the parent class, rather than in the controller.> this would cut code almost in half, for this situation. If there is > anything capable of dynamically generating controller code based on > object classes or even defined variables please let me know!! > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---