# Wondering how to do some clever DRYing up

**URL:** <https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672>\
**Category:** Rails\
**Created:** [May 16, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672 "2006-05-16T18:02:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 16, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672/1 "2006-05-16T18:02:18Z")

</div>

Hi there,  
I’m now getting to the point in my rails app where I can use all of the  
nice basic features like AJAX and engines etc…  
but, I want to get my hands a bit dirtier, whilst learning to DRY up my  
code a bit.

I’m using AJAX quite a bit to dynamically populate/edit/sort has\_many  
relationships in forms, and consequently on a model with many has\_many’s  
I end up repeating functions to deal with adding, sorting & deleting  
different types of record.

For instance I have the folllowing basic sorting controller method for a  
sortable\_element:

def form\_sort\_performers  
i = 1  
params[:performers].each {  
|id|  
Performer.update(id, :position =\> i)  
i = i+1  
}  
render :nothing =\> true  
end

but, I have about 6 things that all need the same basic method  
(Producers, ImagePersonnel etc…)  
is there a way I can somehow move this into the application controller  
and run it generically such that it takes a Model as an argument,  
something like:

def form\_sort(model)  
i = 1  
params[:#{model}].each { # would need to pluralise  
|id|  
#{model}.update(id, :position =\> i)  
i = i+1  
}  
render :nothing =\> true  
end

I’m sure something like this is very possible, and most likely very neat  
and easy to do in ruby/rails - I’m just not sure where to start!  
any pointer gratefully recieved

thanks

dorian

–  
I do things for love or money

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 16, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672/2 "2006-05-16T20:19:39Z")

</div>

Dorian M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> params[:performers].each {  
> def form\_sort(model)  
> any pointer gratefully recieved

I also had a requirement (similar to yours), where I wanted to combine  
the new  
and create action functionality of many controllers into one, so I made  
a base  
controller class and made the other controllers classes inherit from it,  
this  
makes all the inherited controllers get the new and create action.

And for determining the model, I wrote a new model class which takes the  
controller name, and then returns the equivalent model.

The same thing can be also done by a protected method in the application  
controller, which is then called in other inherited controllers with the  
model  
class as an argument.

## HTH.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 17, 2006, 3:47am UTC](https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672/3 "2006-05-17T03:47:48Z")

</div>

I think I found an answer in the rails recipe book, the chapter “write  
code that writes code”, but thanks for the pointers.

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 17, 2006, 4:54am UTC](https://www.ruby-forum.com/t/wondering-how-to-do-some-clever-drying-up/60672/4 "2006-05-17T04:54:45Z")

</div>

There has been some talk on the Ruby-talk mailing list regarding getting  
a  
class from a name.

Thanx to Sergey V. for the class\_by\_name method.  
Put this method into your application\_controller.rb file to be able to  
get a  
class object from a string

def class\_by\_name name  
name.split("::").inject(Object){ |c,n|  
c.const\_get(n)  
}  
end

Then in whatever controller you want put in the following method (the  
argument is either string or symbol)

This follows the convention that you have shown in your post regarding  
the  
pluralisation of the model name in the params hash as a symbol.

def form\_sort( model )  
model = model.to\_s  
params[model.pluralize.downcase.to\_sym].each\_with\_index do |id,index|  
class\_by\_name( model.camelize ).update( id, :position =\> index )  
end  
end

This isn’t tested tho so fingers crossed.
