Tyler
Tyler

Reputation: 2386

Whats the rails way of handling many-to-many?

Ok so I have a photos model and controller that handles photos.. I have two other models restaurant and dish that I want to be able to have photos handled by the photo model. So I created the photos model which as a polymorphic reference to either the dish or restaurant database tables. Should I be using the restaurant controller to handle uploading restaurant photos and same for the dish controller or should I be using the photo controller to handle uploads for both?

Upvotes: 2

Views: 84

Answers (2)

Dan Fox
Dan Fox

Reputation: 729

With this scheme you can think of photos as somewhat complex attributes for Restaurant and Dish records. So I would probably deal with uploading photos in the same place where other attributes for restaurants and dishes are handled. In a RESTful scheme this would happen in the new/edit actions in restaurants_controller and dishes_controller. The whole file uploading process can be helped considerably with a good plugin like CarrierWave.

Upvotes: 0

fl00r
fl00r

Reputation: 83680

The only reason to use polymorphic classes is that their business (or whatever) logic is very same, only difference is their data structure (which has got similar part as well). So you should use or polymorohic models with single controller, or separate classes (models) with separate controllers.

And of course you can use separate controllers for your polymorphic models, but it is out of best practices I think

Upvotes: 1

Related Questions