0

I think this is a simple question, but couldn't figure out my exact answer.

I have an Event listing that has: People, Food, Costs, To-Do, etc. all as different information items. Instead of having to check each corresponding table if a related data item exists, I want to be able to count or grab all the Items that belong to a given Event.

Is this most easily done with HMT? At first I thought STI, but wasn't sure if that made sense because they share very little data in common (besides timestamps and a creator).

dewyze
  • 979
  • 1
  • 7
  • 21
  • Can you tell us how you would do it with HMT relationship? What would be the connection entity in this case? STI is used when you have a parent object that has one or more children (Animal is the parent of Dog, Cat, and Snake). I am not too sure how you would use STI in this case either. – denniss Jul 05 '12 at 20:21
  • Could you do `Event has_many :food_items, :through => :items` and `food_item belongs_to :event, :through => :items`? I am still pretty new to rails, but I am trying to get my database models mostly clear before I go barging ahead. – dewyze Jul 05 '12 at 20:28
  • What is the relationship between food_item and item in this case? Why can't you just do `event has_many :items' or `event has_many :foods` ? – denniss Jul 05 '12 at 20:32
  • For STI, `Item` is the parent of `Person` or `Cost_Item` or `Food_Item`. But other than timestamps, they wouldn't have much in common. The problem (I think?) if I don't have this `Item` intermediary, then I would have to run a bunch of ifs to see if there are `Food_Items` or `Persons`. I am trying to count how items there are before displaying them, to determine the best layout. I didn't know if there was a way to create a catch all, whether it was a parent table or a HMT relationship, or some other tool I don't know about. – dewyze Jul 05 '12 at 20:34
  • If I am overcomplicating, please tell me. I just wanted a way to determine how many `Items` of information an `Event` had regardless of what kind of `Item` they were. – dewyze Jul 05 '12 at 20:36

1 Answers1

1

I think you are overcomplicating your schema design. These are some questions that can be answered using the model given:

  • Given an event, what items were brought to an event?

  • Given an event, who came to the event?

  • Given an event, who brought which item?

- Given an event, what are the activities?

class Event < ActiveRecord::Base
  has_many_and_belongs_to_many :items
  has_many_and_belongs_to_many :todos
  has_many :people, :through => :attendances
end

class Attendance < ActiveRecord::Base
  belongs_to :event
  belongs_to :person
end

class Person < ActiveRecord::Base
  has_many :events, :through => :attendances
end

class Item < ActiveRecord::Base
  has_many_and_belongs_to_many :events
end

class Todo < ActiveRecord::Base
  has_many_and_belongs_to_many :events 
end

This model assumes that:

  • a single item can be brought to two different events. For example, a basketball can be both participate during NBA Finals 2012 and NBA Finals 2011.

  • one activity can be done in two different events. For example, drinking beer can occur at both house party and breakfast

denniss
  • 17,229
  • 26
  • 92
  • 141
  • I think I am not great at asking questions, sorry for that. I actually am going to have many other attributes besides `People, Food Items, etc.` I wanted to know if there was a way to wrap them (with HMT or STI) so that I could count how many there were without having to count the `Persons, Food_Items, Todo, Schedule, Cost, Notes, etc.` individually. – dewyze Jul 12 '12 at 14:39