Profile models

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 :o pen_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 :o pen_ids, :identifier, :unique => true
  end

  def self.down
    drop_table :o pen_ids
  end
end

Both models:

class User < ActiveRecord::Base
  has_many :o pen_ids
end

class OpenId < ActiveRecord::Base
  belongs_to :user
end

A thousand more words:

sano-002

Related posts:

  1. The advantages of OpenID
  2. I'm going to do an experiment today
  3. Converting the ASP.NET MVC project into OpenID
  4. Formating strings in C#, like in Python
  5. The future of OpenID

About J. Pablo Fernández

http://pupeno.om
This entry was posted in other and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">