Skip to content

Instantly share code, notes, and snippets.

@ZackMattor
Created June 2, 2013 02:37
Show Gist options
  • Save ZackMattor/5692411 to your computer and use it in GitHub Desktop.
Save ZackMattor/5692411 to your computer and use it in GitHub Desktop.
Double relationship between users and projects. Projects have many users that can work on them, and projects have one user who is the original creator.
class Project < ActiveRecord::Base
attr_accessible :author, :description, :name, :user_ids
has_and_belongs_to_many :users
belongs_to :author, :class_name => "User", :foreign_key => "author"
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :roles
has_and_belongs_to_many :roles
has_many :projects, through: :ProjectUser
has_many :projects, :class_name => "Project", :foreign_key => "author"
has_many :projects
has_many :blogPosts
def role?(role)
return true
# roles.include? role.to_s
end
def to_s
email
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment