Last active
August 29, 2015 14:02
-
-
Save Ross-Hunter/349304cfccaeca7075e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##Class | |
class User | |
belongs_to :group, inverse_of: :member | |
end | |
class Group | |
# inverse_of because we are adding options to the relationship that prevent | |
# automatic guessing of the inverse | |
has_many :members, class_name: "User", inverse_of: :user | |
end | |
##Migration | |
class AddGroupRefToUsers < ActiveRecord::Migration | |
def change | |
add_reference :user, :group, index: true # adds group_id to user table | |
end | |
end | |
user.group | |
group.members |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment