Skip to content

Instantly share code, notes, and snippets.

@abhishek0
Created August 9, 2013 18:44
Show Gist options
  • Save abhishek0/6196060 to your computer and use it in GitHub Desktop.
Save abhishek0/6196060 to your computer and use it in GitHub Desktop.
class CreateUsers < ActiveRecord::Migration
def change
enable_extension 'uuid-ossp'
create_table :users, id: false do |t|
t.primary_key :id, :uuid, :default => 'uuid_generate_v1()'
t.string :first_name
t.string :last_name
t.string :email
t.string :mobile_number
t.string :locality
t.string :city
t.string :address
t.string :blood_group
t.string :emeregency_number
t.string :user_type
t.integer :dob
t.string :password
t.string :access_token
t.uuid :parent_id
t.timestamps
end
end
end
class User < ActiveRecord::Base
validates :first_name,:last_name, presence: true
validates :email,:mobile_number, presence: true
validates :locality,:city, presence: true
validates :email, uniqueness: true
belongs_to :parent, :inverse_of => :children, class_name: "User"
has_many :children, :inverse_of => :parent, class_name: "User"
before_create :generate_access_token
private
def generate_access_token
access_token = SecureRandom.hex
until User.find_by_access_token(access_token) == nil
access_token = SecureRandom.hex
end
self.access_token = access_token
end
end
admin = User.find_by user_type: "admin"
admin.children # throws ERROR: column users.user_id does not exist
#########################################
franchisee = User.find_by user_type: "franchisee"
franchisee.parent # WORKS!!!
admin = User.create! :first_name => 'admin', :last_name => 'admin',
:email => 'admin@example.com', :mobile_number => '9999911111',
:locality => 'Goregaon', :city => 'Mumbai',
:user_type => 'admin', :password => 'admin'
franchisee = User.create! :first_name => 'franchisee', :last_name => 'franchisee',
:email => 'franchisee@example.com', :mobile_number => '9999911111',
:locality => 'Goregaon', :city => 'Mumbai',
:user_type => 'franchisee', :password => 'franchisee'
franchisee.parent = admin
franchisee.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment