← Back to Upcase

Same field, multiple models

(Kevin McGladdery) #1

My problem:

I’m in the early stages of building a rails application that will track rolls of film. I have at least three models that need to keep track of their film format. There will only be 6-7 possible formats, and it is highly, highly unlikely that this list will ever change.

I could just add a format:string field to every model that requires it, but having the same field duplicated across three models feels wrong for some reason, especially considering the limited number of valid options.

My question:

Is there a better way for three models to store this type of information?



(Ben Orenstein) #2

What about having a FilmFormat model?

Then each of your film models call has_one :film_format.

Make sense?

(Derek Prior) #3

I’d consider making format a model of its own and using a relationship. I know you said there are a limited number of them and that they are unlikely to ever change, but I suspect that at some point in your code you’re going to want to do something interesting based on what type of film format your are dealing with. Creating a model for format would give you a convenient place to hang some of that.

The format column as a string strikes me as a type column which is often a smell that you have an object begging to be extracted.

(Kevin McGladdery) #4

Thanks everyone.

I figured Format would have to be an object of its own, but I wasn’t sure that it warranted an entire table. Thanks to your advice, it seems as if this would be the cleanest and most straightforward way to go.


(Ben Orenstein) #5

Funny thing: I think most programmers err on the side of not creating enough small objects/models.

Just about anything you can put an explicit name to warrants its own object :smile: