Ruby on Rails - How to represent "has_many" with enums?

What is the best solution in a Rails 3.2 application presents something like this:

appointment = Appointment.first
appointment.days_of_the_week = [Monday, Friday]



source to share

2 answers

You can take a look at the Embedded Association RailsCast where Ryan Bates explains how to use bitmasks to avoid using another table, I think it might be a good idea in this case.



There is a Gem called enumerate it that lists the database in such collections. So I would use it to list different days of the week.

With enumerated values, I would create this relationship:

has_many :recurrent_occurrences


And I would put two columns there: appointment_id

and the numeric equivalent of week_day.

If you are using a noSQL database like mongoDB you can add weekdays as an array or inline in a record so no extra table is needed.



All Articles