forked from manveru/ramaze
-
Notifications
You must be signed in to change notification settings - Fork 37
Creating table in model
leucos edited this page Oct 14, 2012
·
2 revisions
If you need a to create a table for a model but don't want to deal with migration, you can create table directly in models by using the Sequel schema plugin.
Here is an example, for a quick and dirty users table :
class User < Sequel::Model
# The schema plugin is required to have access to set_schema
plugin :schema
# Set schema for this model
set_schema do
primary_key :id
varchar :email, :unique => true, :empty => false
varchar :password, :empty => false
boolean :super_powers, :default => false
end
# Create the database table if it doesn't exists
# Also add a user
if ! table_exists?
create_table
create :email => 'admin', :password => '1234', :super_powers => true
end
end
Two remarks here :
- "thou shall not store password like this". See Yorick's post and Using BCrypt for authentication.
- "thou shall use database migrations". Yes, database migration rocks. If your project is not a one short quick and dirty hack, USE Database Migrations.
- Website
- Google Groups
- User Guide
- [#ramaze on the Freenode network] (http://webchat.freenode.net/?channels=ramaze)