Record an association between Users and Messages

Since you're dealing with a relational database, you'll likely want to have different tables relating to one another. For example, let's say you want to have a simple email-like system where users can send one another messages.

First, create a messages table in MySQL:

create table messages (
  pk_id       int not null auto_increment,
  primary key (pk_id),
  subject     varchar(64) not null,
  body        varchar(255) not null,
  author      int not null,
  recipient   int not null,
  date_sent   date not null 

Then add a Message class to the end of tutorial_setup.rb.

class Message < Lafcadio::DomainObject
  string        'subject'
  string        'body'
  domain_object User, 'author'
  domain_object User, 'recipient'
  date          'date_sent'

As before, you leave out the pk_id field.

Once you've defined the Message class, you can treat instances of User as values of the fields Message#author and Message#recipient.

require 'tutorial_setup'

john = User[1]
five_years_ago = - ( 365 * 5 )
jane =
	'birthday' => five_years_ago
  'email' => '',
  'first_name' => 'Jane',
  'last_name' => 'Doe',
  'password' => 'jane_pass',
message_body = "Hey, Jane,\n\nWanna go to the movies on Saturday?"
message =
  'subject' => 'hey',
  'body' => message_body,
  'author' => john,
  'recipient' => jane,
  'date_sent' =>

With this code Lafcadio will save all the appropriate id numbers in the right fields in MySQL.