News for hackers. Stuff that matters.
Re:Things I wished more developers knew about databases by lovegoblin on Wednesday April 22, 2020 @12:40PM (#22947452)
> Rails' implementation of enums is a good example of this.

The advantage of ActiveRecord enums (vs db-native) is that you can change the list of valid values without having to run a whole ALTER TYPE - and all the overhead that would entail in doing without downtime in a production db.

Re:Things I wished more developers knew about databases by petepete on Thursday April 23, 2020 @08:19AM (#22955133)
And the disadvantage is that you need to look at the code to understand the data.

Some things change infrequently enough for DB native to be the better solution.

Re:Things I wished more developers knew about databases by evsasse on Thursday April 23, 2020 @08:42AM (#22955296)
A nice "trick" is to declare your enum column on the database as a string, and your enum in the code as a hash with string values. So you can have self-explained data saved onto the database, and all the niceties Rails gives you from the enum.

  enum object_type: { review: "review", purchase: "purchase", offer: "offer", reward: "reward" }
  # OR
  enum action: TYPES.map { |type| [type.to_sym, type.to_s] }.to_h
Re:Things I wished more developers knew about databases by rietta on Thursday April 23, 2020 @02:09PM (#22958987)
Nice! I've done #1, but #2 is a neat trick.