# Help refactoring this controller method

**URL:** <https://www.ruby-forum.com/t/help-refactoring-this-controller-method/206557>\
**Category:** Rails\
**Created:** [May 4, 2011, 6:57pm UTC](https://www.ruby-forum.com/t/help-refactoring-this-controller-method/206557 "2011-05-04T18:57:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 4, 2011, 6:57pm UTC](https://www.ruby-forum.com/t/help-refactoring-this-controller-method/206557/1 "2011-05-04T18:57:37Z")

</div>

This is in a CMS type application, and the controller is for creating a  
‘page’.

I have:

def create

category = Category.find( params[:categor\_id] )  
text = params[:article\_body]

article = Article.new

article.category\_id = category.id  
article.ip\_address = request.remote\_ip  
artcle.original\_text = text  
article.formatted\_text = ## some markdown call here to parse text  
article.user\_id = current\_user.id  
article.is\_approved = true

article.save  
end

Now the problem is I do the same sort of thing for updates and other  
places,  
how would you suggest I refactor this?

---

<div class="post-metadata">

**Author:** ![S\_Ahmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@S\_Ahmed](https://www.ruby-forum.com/u/S_Ahmed)\
**Post date:** [May 4, 2011, 7:18pm UTC](https://www.ruby-forum.com/t/help-refactoring-this-controller-method/206557/2 "2011-05-04T19:18:01Z")

</div>

You could create a method in the Article class that handles all this.

class Article

def self.custom\_create(category, user, text, ip\_address, is\_approved)  
a = Article.new  
a.category\_id = category.id  
a.ip\_address = ip\_address  
a.user\_id = user.id  
a.original\_text = text  
a.formatted\_text = text.markdowned()  
a.is\_approved = is\_approved  
a.save  
end

end

Then you could just call this from your controller:

Article.custom\_create(category, current\_user, params[:article\_body],  
request.remote\_ip, true)

---

<div class="post-metadata">

**Author:** ![S\_Ahmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@S\_Ahmed](https://www.ruby-forum.com/u/S_Ahmed)\
**Post date:** [May 4, 2011, 10:28pm UTC](https://www.ruby-forum.com/t/help-refactoring-this-controller-method/206557/3 "2011-05-04T22:28:16Z")

</div>

Maybe what you need is make use of rails\_best\_practices gem

> **[GitHub - flyerhzm/rails\_best\_practices: a code metric tool for rails projects](https://github.com/flyerhzm/rails_best_practices)**
>
> a code metric tool for rails projects. Contribute to flyerhzm/rails\_best\_practices development by creating an account on GitHub.

once you install it and run in your rails project it will discover this  
kind of problematic controllers and tell you about ‘code smell’. Plus,  
it will point you to solution on [http://rails-bestpractices.com/](http://rails-bestpractices.com/).  
Anyway, if you run this gem in console, then just search by ‘problem  
message’ on [rails-bestpractices.com](http://rails-bestpractices.com) site and you will find post which  
explain how to remedy ‘code smell’ how they call it.

What Tim S. replied you is called - Factory Method:

[http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method](http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method)

* * *

> **[Eugen.co may be for sale - PerfectDomain.com](https://perfectdomain.com/domain/eugen.co)**
>
> Checkout the full domain details of Eugen.co. Click Buy Now to instantly start the transaction or Make an offer to the seller!
