Skip to content

Instantly share code, notes, and snippets.

@kxhitiz
Created June 13, 2011 03:33
Show Gist options
  • Save kxhitiz/1022286 to your computer and use it in GitHub Desktop.
Save kxhitiz/1022286 to your computer and use it in GitHub Desktop.
my account and user model overview
Account Model(account.rb):
class Account < ActiveRecord::Base
has_many :projects, :dependent => :destroy
has_many :users, :dependent => :destroy
accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true
validates :subdomain, :presence => true, :uniqueness => true
validates_exclusion_of :subdomain,
:in => AppVariables::RESERVED_DOMAINS,
:message => "has already been taken."
end
# == Schema Information
#
# Table name: accounts
#
# id :integer(4) not null, primary key
# company_name :string(255)
# active :boolean(1)
# time_zone :string(255)
# subdomain :string(255)
# working_days :string(255)
# created_at :datetime
# updated_at :datetime
---------------------------------------------------------------------------------------------------------
User Model(user.rb):
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :encryptable
# Setup accessible (or protected) attributes for your model
attr_accessible :login, :first_name, :last_name, :username, :email, :password, :password_confirmation, :remember_me
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# relationships
belongs_to :account
has_many :projects_users
has_many :projects, :through => :projects_users
#validations
validates_uniqueness_of :username, :scope => :account_id, :case_sensitive => false
validates_uniqueness_of :email, :scope => :account_id, :case_sensitive => false
protected
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
end
end
# == Schema Information
#
# Table name: users
#
# id :integer(4) not null, primary key
# account_id :integer(4) not null
# first_name :string(255)
# last_name :string(255)
# address :string(255)
# city :string(255)
# type :string(255)
# username :string(255) not null
# email :string(255) not null
# crypted_password :string(255) not null
# password_salt :string(255) not null
# persistence_token :string(255) not null
# single_access_token :string(255) not null
# perishable_token :string(255) not null
# created_at :datetime
# updated_at :datetime
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer(4)
# avatar_updated_at :datetime
# is_active :boolean(1) default(TRUE), not null
# deliverable_notification :integer(4) default(0)
# adjustment_notification :integer(4) default(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment