Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete GameNight specs and associations #46

Merged
merged 2 commits into from
Jan 6, 2016

Conversation

npauzenga
Copy link
Owner

Hey @enriikke one small question below...

belongs_to :organizer, class_name: :User, foreign_key: :organizer_id

has_many :user_game_night_attendees
has_many :users, through: :user_game_night_attendees
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enriikke you'd mentioned the possibility of making these more generic (like Memberships in the case of Groups). What would that look like? Here, our UserGameNightAttendees has a user_id and a game_night_id. If you wanted something more versatile would you just have attributes like id_1 and id_2 so that a Membership or Attendance could apply to any type of model?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @npauzenga, making Membership or Attendance more generic would make it easier if we need to reutilize that same functionality. If we don't have a case where we would need to reutilize it then keeping them specific works great 😄.

If we wanted to generalize them, we could use polymorphic associations 👻 🐍. These shapeshifting creatures are documented here. ActiveRecord can handle everything for us but on a high level, all it's doing is adding two attributes to the table: an id and a type. We tell AR the name of our polymorphic friend, for example attendable and it uses that to add the column attendable_id, then it automatically figures out the type of object based on its class name and uses that for the attendable_type column. So when we query for user.attendances it's checking something like:

# This is probably not exactly how AR's implementation looks
Attendance.where(attendable_id: user.id, attendable_type: user.class.name.downcase)

@enriikke
Copy link
Collaborator

enriikke commented Jan 5, 2016

Awesome work @npauzenga!!! :shipit: :neckbeard:

@npauzenga npauzenga removed the question label Jan 5, 2016
npauzenga added a commit that referenced this pull request Jan 6, 2016
complete GameNight specs and associations
@npauzenga npauzenga merged commit fc37fa2 into staging Jan 6, 2016
@npauzenga npauzenga deleted the bugs/game-night-foreign_keys branch January 6, 2016 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants