Skip to content

Instantly share code, notes, and snippets.

@kany
Created March 27, 2014 15:11
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 kany/9809730 to your computer and use it in GitHub Desktop.
Save kany/9809730 to your computer and use it in GitHub Desktop.
Using ActiveRecord outside of a Rails app
require 'active_record'
require 'mysql2'
# Database Connection
ActiveRecord::Base.establish_connection(
adapter: 'mysql2', # or 'postgresql' or 'sqlite3'
host: 'localhost',
database: 'chc_user_auth_direct_login',
username: 'root',
password: ''
)
# Database Class
class User < ActiveRecord::Base
rolify
# 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 :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
# Now do stuff with it
puts SomeClass.find :all
some_class = SomeClass.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment