0

I have a model which may be associated with zero or more static values. For example, movies and genres (horror, action, etc.) I want to make an genres array on my model but I'm not sure the best way to reflect this in the database.

I am using Rails/AR 4 and Postgres along with the pg_search gem.

I can go the classic route of creating a genres table and creating a join table between the two. However, this seems a little bit like overkill because genres do not change very often.

I could create a genre enum type and then create an array of genres on the the model but I'm not sure how well Active Record supports this sort of mechanism.

I will need to be able to search for movies based on genre as well as other criteria (keyword, length, rating, etc.)

Any best practices and/or resources would be greatly appreciated. Am I missing a simpler or more straight forward solution? Do any of the above fail to solve my problem in a way I am not anticipating?

Drew
  • 15,158
  • 17
  • 68
  • 77

1 Answers1

2

I would recommend going with an other table rather than arrays.

Arrays are fine if you want to have free text. Here, you really want to have resource. If you have a "scifi" genre and, after realizing nobody understands what scifi is, decide to rename it to "scifi / fantastic", you now have to iterate on all your movies and check if the scifi name was used.

With a foreign table, that's easy. Your movie has a genre_id column, and your genre row has a name column that you can change to change all movies.

A good use of an array would be to describe detailed and adhoc attributes of the film, like atmosphere traits, innovative specificities, and anything that is a list of thing that would probably not be used for several movies and should be redacted enough.

Also note postgres array support is not perfect in rails, as for now, and you may have to deal with a few glitches.

kik
  • 7,867
  • 2
  • 31
  • 32