Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created February 12, 2014 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/8959710 to your computer and use it in GitHub Desktop.
Save jeremyf/8959710 to your computer and use it in GitHub Desktop.
Thinking about how to model connecting a Group to a User
class Hydramata::Connection
include Virtus.model
include ActiveModel::Validations
extend ActiveModel::Naming
attribute :current_user, User
attribute :group_id, String
attribute :user_id, String
attribute :role, Sting
validates :role, inclusion: { ['manager', 'editor', 'soon?']}
def group
@group ||= Group.find(group_id)
end
def user
@user ||= User.find(user_id)
end
def save
valid? ? persist : false
end
private
def persist
group.add_member(user) && user.add_group(group)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment