-
-
Save chsh/5517937 to your computer and use it in GitHub Desktop.
1. Use SecureRandom.uuid instead of UUIDTools.
2. Belonging models should specify string type refering Site.
3. Apply Ruby 1.9 or higher syntax. ;-)
This file contains hidden or 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
# app/models/link.rb | |
class Link < ActiveRecord::Base | |
belongs_to :site | |
end |
This file contains hidden or 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
# db/migrate/20110422210841_create_sites.rb | |
# 1. :id => false | |
# 2. :uuid | |
# | |
class CreateSites < ActiveRecord::Migration | |
def self.up | |
create_table :sites, id: false do |t| | |
t.string :uuid, limit: 32, primary: true | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :sites | |
end | |
end |
This file contains hidden or 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 CreateLinks < ActiveRecord::Migration | |
def self.up | |
create_table :links do |t| | |
t.string :site_id, limit: 32 | |
t.timestamps | |
end | |
add_index :links, :site_id | |
end | |
def self.down | |
drop_table :sites | |
end | |
end |
This file contains hidden or 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
# app/models/site.rb | |
class Site < ActiveRecord::Base | |
include Extensions::UUID | |
has_many :links | |
end |
This file contains hidden or 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
# app/models/extensions/uuid.rb | |
# | |
module Extensions | |
module UUID | |
extend ActiveSupport::Concern | |
included do | |
self.primary_key = 'uuid' | |
before_create :generate_uuid | |
def generate_uuid | |
self.id = SecureRandom.uuid.gsub(/-/, '') | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you sure
primary: true
is working? When I run your migration, theuuid
column is nullable and the table has no primary key. I'm running Rails 3.2.13, and this happens with both mysql and sqlite. This is the generated table: