0

I have a problem that I can't solve. The problem is that I've got one user-model, and one event-model and in the event model a user can have multiple role's. We can also add attendants either an existing user, or create a fake user that can be converted (just by a flag in the db) to a real user.

So the diffrent user-type are:
A existing  user who has created the event (this is a one-to-one relationship)
A existing  user who is admin for the event
A existing user who has paid money on the event
A existing user who attended the event
and lastly a person who attended the event but isn't a user yet but has to be shown, so a fake-user would do here..

When I add the attending users/fake users I use nested forms.

So does anyone have a idea for how to solve this? I have tried with habtm, has_many :through with polymorphism and STI but with no luck. Thanks in advance!

jonepatr
  • 7,769
  • 7
  • 30
  • 53

1 Answers1

1

You want a multi-habtm relationship, but I don't think you need it. You can't make this polymorphic. Here is my suggested setup:

For your events table columns:

  • id
  • creator_id (FK to users.id)
  • (event-specific fields)

Keep your existing users tables as is...

Now, the fun part - the users_events table:

  • id
  • user_id (FK users.id)
  • event_id (FK events.id)
  • is_admin? (boolean)
  • is_paid? (boolean)
  • is_attended? (boolean)

As for handling your non-existent attending user problem - if you aren't concerned with the user's data, just leave user_id NULL where applicable. If you want the data, I suggest using what you already suggested - a boolean flag.

sethvargo
  • 26,739
  • 10
  • 86
  • 156
  • This complicates it a bit with my nested forms, how do i solve that issue? (I use nested forms for the attending persons who aren't users yet) – jonepatr Jan 11 '11 at 10:41
  • have an Event accept nested attributes for a user. then, I believe the key is event[user_attributes][][is_admin?], etc. – sethvargo Jan 11 '11 at 13:29
  • but is the "is_admin?" a user_attribute? isn't it a users_events_attribute? – jonepatr Jan 12 '11 at 16:04
  • a user is an admin FOR AN EVENT. If you use fields_for it will build the necessary association for you – sethvargo Jan 12 '11 at 19:09