# Refactor question

**URL:** <https://discuss.rubyonrails.org/t/refactor-question/68349>\
**Category:** rubyonrails-talk\
**Created:** [January 10, 2014, 2:55pm UTC](https://discuss.rubyonrails.org/t/refactor-question/68349 "2014-01-10T14:55:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 10, 2014, 2:55pm UTC](https://discuss.rubyonrails.org/t/refactor-question/68349/1 "2014-01-10T14:55:27Z")

</div>

Hi, The code below works perfectly but is so repetitive I feel there must be a standard way to refactor it in this situation. I am learning on my own so am wondering how more experienced programmers would do this. Any examples would be appreciated.

def reset\_to\_previous &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contents\_history = Profile.find(:first, conditions: ["user\_id = ?", current\_user.id])

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if contents\_history[:last\_minisection\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:minisection\_id] = contents\_history[:last\_minisection\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subsection\_id] = contents\_history[:last\_subsection\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_id] = contents\_history[:last\_section\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_id] = contents\_history[:last\_chapter\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_id] = contents\_history[:last\_book\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_title] = Book.find(session[:book\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_title] = Chapter.find(session[:chapter\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_title] = Section.find(session[:section\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subsection\_title] = Subsection.find(session[:subsection\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:minisection\_title] = Minisection.find(session[:minisection\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "contents", :action =\> "index\_minisections", :subsection\_id =\> session[:subsection\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif contents\_history[:last\_subsection\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subsection\_id] = contents\_history[:last\_subsection\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_id] = contents\_history[:last\_section\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_id] = contents\_history[:last\_chapter\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_id] = contents\_history[:last\_book\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_title] = Book.find(session[:book\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_title] = Chapter.find(session[:chapter\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_title] = Section.find(session[:section\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subsection\_title] = Subsection.find(session[:subsection\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "contents", :action =\> "index\_subsections", :section\_id =\> session[:section\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif contents\_history[:last\_section\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_id] = contents\_history[:last\_section\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_id] = contents\_history[:last\_chapter\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_id] = contents\_history[:last\_book\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_title] = Book.find(session[:book\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_title] = Chapter.find(session[:chapter\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:section\_title] = Section.find(session[:section\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "contents", :action =\> "index\_sections", :chapter\_id =\> session[:chapter\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif contents\_history[:last\_chapter\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_id] = contents\_history[:last\_chapter\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_id] = contents\_history[:last\_book\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_title] = Book.find(session[:book\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:chapter\_title] = Chapter.find(session[:chapter\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "contents", :action =\> "index\_chapters", :book\_id =\> session[:book\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif contents\_history[:last\_book\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_id] = contents\_history[:last\_book\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:book\_title] = Book.find(session[:book\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "contents", :action =\> "index\_books", :subject\_id =\> session[:subject\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif contents\_history[:last\_subject\_id].present? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_id] = contents\_history[:last\_subject\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:subject\_title] = Subject.find(session[:subject\_id]).title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:controller =\> "subjects", :action =\> "index") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

Thanks,

Dave

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson2](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dave\_Aronson2](https://discuss.rubyonrails.org/u/Dave_Aronson2)\
**Post date:** [January 10, 2014, 3:08pm UTC](https://discuss.rubyonrails.org/t/refactor-question/68349/2 "2014-01-10T15:08:01Z")

</div>

It looks like the general pattern is to check if a given thing is present, and if so, do a bunch of things, all of which are included in the previous bunch (except of course for the first). So, maybe instead of an else-chain, just make them all independent ifs, and have them do ONLY what is not included in the next one.

-Dave

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@Tamara\_Temple](https://discuss.rubyonrails.org/u/Tamara_Temple)\
**Post date:** [January 11, 2014, 2:53am UTC](https://discuss.rubyonrails.org/t/refactor-question/68349/3 "2014-01-11T02:53:06Z")

</div>

What version of Rails is this? Rails 2? Did Rails 3 use conditions? (It has been sooo lonnnggg)

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 11, 2014, 3:35am UTC](https://discuss.rubyonrails.org/t/refactor-question/68349/4 "2014-01-11T03:35:42Z")

</div>

tamouse m. wrote in post #1132818:
