# Help needed to DRY helpers and proper MVC with Ruby

**URL:** <https://www.ruby-forum.com/t/help-needed-to-dry-helpers-and-proper-mvc-with-ruby/110586>\
**Category:** Ruby\
**Created:** [July 27, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/help-needed-to-dry-helpers-and-proper-mvc-with-ruby/110586 "2007-07-27T21:04:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 27, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/help-needed-to-dry-helpers-and-proper-mvc-with-ruby/110586/1 "2007-07-27T21:04:34Z")

</div>

Hello.

I’ve got working code, but I don’t think that it’s done properly so any  
help to make this more efficient and most of all, correct, would be so  
very appreciated.

In my users\_helper.rb file

def generate\_sub\_nav  
if session[:user]  
sub\_nav\_menu = []  
user = User.find(session[:user])  
for role in user.roles  
for right in role.rights  
if right.on\_menu  
sub\_nav\_menu \<\< right.id  
end  
end # for right  
end # for role  
end # if  
sub\_nav\_menu.sort!  
end # def

In my application\_helper.rb file for the layout and display

def show\_sub\_nav(rights)  
html\_out = ‘

| ’

for right in rights  
right\_array = Right.find(:all, :conditions =\> [“id = ?”, right])  
for menu in right\_array  
html\_out \<\< link\_to\_unless\_current (menu.name,  
:controller =\> menu.controller, :action =\> menu.action)  
end  
end #for right in rights

html\_out \<\< ‘

 |

’  
html\_out  
end

In my controllers (not including the user\_controller)

helper :users

There’s got to be a better, more Ruby like way to do this.

THANKS!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 27, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/help-needed-to-dry-helpers-and-proper-mvc-with-ruby/110586/2 "2007-07-27T21:19:41Z")

</div>

> I’ve got working code, but I don’t think that it’s done  
> properly so any help to make this more efficient and most of  
> all, correct, would be so very appreciated.

Your question is perhaps best asked on the rails list. There are a  
couple of different things you could do to make your ActiveRecord usage  
more efficient, but this isn’t the forum for that.

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

As far as making this code more rubyish, most rubyists seem to prefer  
iterators with blocks, e.g.:

user.roles.each do |role|  
role.rights.each do |right|  
…  
end  
end

to for loops.

- donald
