Pergunta

I'm developing a simple app for creating and managing events such as birthdays, weddings etc. All of the different events have different attributes and could be very different. I'm a beginner in rails but I've done the Rails for Zombies course so I know how to create models/controllers. However, I'm stuck now when I want a common identifier for all the events, like a key. It should be possible to search for events using a code/identifier and I don't know how to do this.

I was thinking of having an Event model which would contain the identifier, ex 'JamesBDay'. Then each of the different event model (Birthday/Wedding etc) would keep it's respective Event model. Am I thinking right and how do I do this using Rails 'generate' and 'db:migrate' commands?

And when this is accomplished, how do i search for any type of event using the identifier in their Event attribute?

An explaining picture below An explaining picture below

Thanks!

Foi útil?

Solução

This sounds like a perfect problem for Single Table Inheritance. Essentially you would have a user that has_many events. Create an events table and add a field called "type". From there you create other models that subclass "Event" and NOT "ActiveRecord::Base". These will all be stored in the same table, but will be unique objects that can have their own methods. This allows you to parse through the events as an entire list "Event.all" and then find out the type there, or you can parse through individual event types with "Wedding.all".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top