# Design - Best practices on managing many-to-many controllers?

**URL:** <https://www.ruby-forum.com/t/design-best-practices-on-managing-many-to-many-controllers/187121>\
**Category:** Rails\
**Created:** [April 17, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/design-best-practices-on-managing-many-to-many-controllers/187121 "2010-04-17T15:31:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [April 17, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/design-best-practices-on-managing-many-to-many-controllers/187121/1 "2010-04-17T15:31:30Z")

</div>

Hi,

This may seem a silly question, but I didn’t find an elegant solution  
to manage a user’s tag collection. Perhaps I’m missing something, I’d  
like to know if there is a better way to handle it are.

The configuration is pretty standard stuff:

user.rb:  
has\_many :user\_tags, :dependent =\> :destroy  
has\_many :tags, :through =\> :user\_tags

tag.rb:  
has\_many :user\_tags, :dependent =\> :destroy  
has\_many :users, :through =\> :user\_tags

user\_tag.rb:  
belongs\_to :user  
belongs\_to :tag

(tags also have many-to-many associations w/ other resources).

I would like users to have a separate page to manage their tags.

I have thought about some solutions:

- Manage the tags using different templates for user actions. Problem:  
doesn’t scale, will add complex logic to controllers.

- Extend the users resource with two new member actions. Problem:  
doesn’t scale, users will have many more collections.

- Create a controller for user\_tags, nest the resource under users.  
This is what I’m using now. It doesn’t feel right to manipulate the  
user\_tags table directly, Rails already has logic for things like:

- Create a non-restful controller, maybe to manage all users’  
collections, keeping things in one place. Add routes manually. In  
particular, leverage the construct:

Any thoughts?

Thanks,

–  
Adriano

---

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [April 17, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/design-best-practices-on-managing-many-to-many-controllers/187121/2 "2010-04-17T18:56:21Z")

</div>

I decided to create a restful controller for the tags, nested in the  
users resources, and not associated with any model:

resources :users do  
resources :utags, :only =\> [:index, :create, :destroy]  
end

I’m using these routes in the views:

\<%= link\_to ‘destroy’, user\_utag\_path(@user, tag), :method  
=\> :delete %\>

\<%= form\_tag user\_utags\_path(@user) do %\>

And in the controller (no error handling for now):

```
create:
tag = Tag.find_or_create_by_name(params[:name])
@user.tags << tag

destroy:
tag = Tag.find(params[:id])
@user.tags.delete(tag)

```

I think that this is a satisfactory solution.

–  
Adriano
