Created
August 9, 2013 18:44
-
-
Save abhishek0/6196060 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
admin = User.create! :first_name => 'admin', :last_name => 'admin', | |
:email => '[email protected]', :mobile_number => '9999911111', | |
:locality => 'Goregaon', :city => 'Mumbai', | |
:user_type => 'admin', :password => 'admin' | |
franchisee = User.create! :first_name => 'franchisee', :last_name => 'franchisee', | |
:email => '[email protected]', :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