# Design modeling question

**URL:** <https://www.ruby-forum.com/t/design-modeling-question/134988>\
**Category:** Rails\
**Created:** [April 16, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/design-modeling-question/134988 "2008-04-16T21:26:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [April 16, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/design-modeling-question/134988/1 "2008-04-16T21:26:24Z")

</div>

controllers  
ManufacturersController  
ProductsController  
ClassifiedsController  
UsersController

models (parent models)  
Manufacuturer has\_one Logo  
Product has\_one product\_picture  
Classified has\_many classified\_photos  
User has\_one avatar

more models (STI)  
Logo  
Product\_Picture  
Classified\_photos  
Avatar

1- Do I create one Controller called ImageUploadController and put all  
the custom methods (each type of picture has its own characteristics),  
where I will also need to have custom routes such as:  
add\_prodcut\_logo, delete\_product\_logo, etc…

2- Add custom methods, and custom named routes in the parent models

3- Or, just create a separate controller for each such as  
LogoController, ProductPhotoController, ClassifiedPhotoController and  
use nested routes, each of the pictures will be operated on within  
their parent model.

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [April 17, 2008, 3:16am UTC](https://www.ruby-forum.com/t/design-modeling-question/134988/2 "2008-04-17T03:16:03Z")

</div>

mabed wrote:

> 1- Do I create one Controller called ImageUploadController and put  
> all  
> the custom methods (each type of picture has its own characteristics),  
> where I will also need to have custom routes such as:  
> add\_prodcut\_logo, delete\_product\_logo, etc…
> 
> 2- Add custom methods, and custom named routes in the parent models

The non-default route system exists so you don’t need to name URI paths  
after  
controllers. This lets you put the logic wherever you like, independent  
from the  
controllers.

> 3- Or, just create a separate controller for each such as  
> LogoController, ProductPhotoController, ClassifiedPhotoController and  
> use nested routes, each of the pictures will be operated on within  
> their parent model.

Which solution leads to the least code, and with the most similar code  
grouped  
together? I suspect that a controller in charge of images would have a  
few  
private methods - maybe a few helper methods - for the heavy lifting.

Also, remember to write unit tests before you write the tested code.  
This  
(generally) forces you to test all your code, and it allows you to move  
code  
around. You should never be stuck with one pattern. You should be free  
to start  
with something that sucks but at least works, so you can improve that  
over time.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [April 17, 2008, 5:37am UTC](https://www.ruby-forum.com/t/design-modeling-question/134988/3 "2008-04-17T05:37:36Z")

</div>

The way I handled it is to think about it this way:

A Product Logo is really just part of a Product, so it should be  
handled by the create/update on the products controller.

Then, within the create method on products\_controller, I’d do  
something like:

@product = Product.new(params[:product])  
@product.build\_product\_logo(params[:product\_logo])

That will create the logo and assign it to product via the  
association.

For my app, the logo uploads are all within the context of something  
else. I.e. there isn’t ever a form _just_ for logos. They are always  
part of a Product or User or whatever. This also then puts the logic  
of what type of logo into the realm of the “thing” that cares… i.e.  
a Product needs ProductLogo so it’s create method uses  
“build\_product\_logo”.

-Danimal
