Create a table, and corresponding domain class

Object types that are represented in the database are called domain objects. You need to write a class definition for each different domain object type.

First, so we have a domain object type, let's create a User type. Log into MySQL and create a users table:

create table users (
  pk_id       int not null auto_increment,
  primary key (pk_id),
  first_name  varchar(64),
  last_name   varchar(64),
  email       varchar(64) not null,
  password    varchar(64) not null,
  birthday    date
);

Note that the primary key is named pk_id. Lafcadio is fairly flexible about what your tables look like, but it requires each table to have a numeric primary key. It assumes that it's going to be named pk_id, and that can be changed, but it's a little less work to go with the default.

Although we're creating a new table in this example, Lafcadio can also be integrated with pre-existing tables with only a little more work.

Next, write a child of DomainObject which defines the fields. Let's put this at the end of tutorial_setup.rb:

class User < Lafcadio::DomainObject
  string 'first_name'
  string 'last_name'
  string 'email'
  string 'password'
  date   'birthday'
end

You write one field directive for every field in the table, except for pk_id. For style reasons, Lafcadio expects the table name to be the plural (users) and expects the domain class type to be singular (User). You can override this assumption if you like.