Skip to content

Instantly share code, notes, and snippets.

@Ross-Hunter
Last active August 29, 2015 14:02
Show Gist options
  • Save Ross-Hunter/349304cfccaeca7075e8 to your computer and use it in GitHub Desktop.
Save Ross-Hunter/349304cfccaeca7075e8 to your computer and use it in GitHub Desktop.
##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