views:

102

answers:

3

Let's assume this model:

Movie
- Title: String
- Has many:
    - Alternative Title: String

My questions is, how should I store the alt. title attribute? I am deciding between three approaches:

  • Separate AR model: probably an overkill
  • CSV in a signle DB column
  • Serialized array in single DB column

The latter two seems logically equivilent. I am leaning towards the CSV approach. Can anyone give some advise on this? What would be the implications on speed and searchability?

+2  A: 

If a movie can have many titles, it makes most sense to have a Title model and give the Movie model a has_many :titles relation, especially if you later on decide to add more metadata about titles. It may seem like overkill, but I think it will be the least hassle in the long run. Furthermore, I think that a movie's "main" title should be a Title object as well, perhaps with an is_main_title or similar attribute to distinguish it from the others.

Jordan
Hi, I agree that it is USUALLY a good idea to have a separate model, but I still think it's a bit overdone. Maybe this is not the best example to illustrate the problem, because a title might indeed gain other attributes overtime (e.g. Language...). But I believe there are cases that the data type is just a simple number or a string, and you are very certain that it will not gain other attributes in the forseeable future.
Godfrey
Even if it will never have additional attributes, it still seems to me like it's the equivalent of one-to-many relations in a database. For data normalization, you have a separate table, even if nothing is in that table but two columns.
JacobM
A: 

If most of the time you only use the primary title, I'll go with your CSV option.

If most of the time you use all the titles, I'll put all the titles (primary and secondary) inside a single CSV column (named "titles") and just get the first when the primary is needed (with a helper function).

Why? Because it makes things simple- and if the time has come, like Jordan said, that you need another attribute you can always migrate to a separate model.

Until then, YAGNI.

amikazmi
A: 

I would also vote for a separate model even though it seems like overkill it will allow you to basically follow the Rails way the easiest. However, if you choose not to reap the benefits of all the baked in magic associated with associations, then I would recommend YAML or JSON over CSV. CSV is quite simple, but Rails has baked in support for YAML serialization and would probably be the easiest solution. Check out RDoc on #serialize. For the given example this would basically amount to:

class Movie < ActiveRecord::Base
  serialize :alternate_titles
end

With that, Rails would handle a lot of the drudgery for you and you'll have a nice array of alternate titles always available.

andykram