How to store year in Rails app

database-design, datetime, ruby-on-rails

Solution

It depends on the range of dates that are possible. If your dates go from BC to far future (negative to more then 4 digits), it would be easiest to store an integer (just for sorting reasons). This is also true if you want to make calculations or compare dates.

Otherwise I would probably go with a string.

Using a date field use January 1st and extract the year part when showing.

Problem

I want to just store the year in one of my models. At the moment I just have a database field called year of type date, but it seems to want to take a whole date (yyyy-mm-dd) rather than just a year. What's the best way to store this? In a date field but using some way of just getting it to store the date bit (in which case, how?), or in an int field, or a string field or what? In some ways using an int field feels wrong, but it might be the easiest.

Original source