The profiles in Sano are built out of two models:
- User
- OpenId
The reason to have an OpenId model and not just one field in the User model is that I like giving users the possibility of defining more than one OpenId provider. StackOverflow does that and since I use my own OpenId server, the day it was down, I was really grateful I could still use my myOpenId account to log in.
So I created the two models:
./script/generate model user name:string email:string height:float gender:boolean birthday:date
./script/generate model open_id user_id:integer identifier:string display_identifier:string
and then I used Matthew Higgins’s foreigner to define the foreign keys. The end result follows.
Migration for users:
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.string :email t.float :height t.boolean :gender t.date :birthday t.timestamps end end def self.down drop_table :users end end
Migration for OpenIds:
class CreateOpenIds < ActiveRecord::Migration def self.up create_table :open_ids do |t| t.integer :user_id, :null => false t.string :identifier, :null => false t.string :display_identifier t.timestamps t.foreign_key :users end add_index :open_ids, :identifier, :unique => true end def self.down drop_table :open_ids end end
Both models:
class User < ActiveRecord::Base has_many :open_ids end class OpenId < ActiveRecord::Base belongs_to :user end
A thousand more words:
Leave a Reply