This document compares the naming conventions between ActiveRecord and DataMapper for Ruby on Rails.
With ActiveRecord, table names are automatically pluralized and primary keys are always integers. Model names end with the word "Model" by convention but it is not required.
DataMapper uses similar conventions to ActiveRecord for table and primary key naming. Model names do not require any suffix. DataMapper also supports natural primary keys, composite primary keys, and lazy loading of attributes.
3. Über uns
# internes Entwicklerteam von Sales-Lentz
# IBEs für Reisen, Bustickets, Eventtickets
# seit 2006 entwickeln wir mit Ruby on Rails
# Buch Ruby on Rails 2 Galileo Press
# http://railsbuch.de
# http://twitter.com/ajnato
# http://twitter.com/HusseinMorsy
3
11. ActiveRecord Migrations
# Neue Tabelle erstellen
class CreateShoes < ActiveRecord::Migration
def self.up
create_table :shoes do |t|
t.string :brand
t.float :price
t.timestamps
end
end
def self.down
drop_table :shoes
end
end
rake db:migrate
11
12. ActiveRecord Migrations
# Neues Feld hinzufügen
class AddAvailableToShoes < ActiveRecord::Migration
def self.up
add_column :shoes, :available, :boolean
end
def self.down
remove_column :shoes, :available
end
end
rake db:migrate
12
13. DataMapper Migrations
# Neue Tabelle erstellen
class Shoe
include DataMapper::Resource
property :id, Serial
property :brand, String
property :price, Float
end
# Tabelle shoes erstellen
# Vorsicht: Daten gehen verloren
Shoe.auto_migrate!
# alle Tabellen erstellen
# Vorsicht: Daten gehen verloren
DataMapper.auto_migrate!
# In Datamapper gibt es auch Migrations, ähnlich wie in ActiveRecord
# siehe dm-migrations
13
14. DataMapper Migrations
# Neues Feld hinzufügen
class Shoe
include DataMapper::Resource
property :id, Serial
property :brand, String
property :price, Float
property :available, Boolean
end
# Vorsicht: Daten gehen verloren
Shoe.auto_migrate!
14
24. ActiveRecord Validations
# Model ohne Validierung
class Woman < ActiveRecord::Base
end
24
25. ActiveRecord Validations
# Model mit Validierung
class Woman < ActiveRecord::Base
validates_presence_of :name
validates_length_of :title, :within => 2..20
validates_format_of :email,
:with => /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
validates_uniqueness_of :email
end
25
26. DataMapper Validations
# Model ohne Validierung
class Woman
include DataMapper::Resource
property :id, Serial
property :name, String
property :title, String
property :email, String
end
26
27. DataMapper Validations
# Model mit Validierung
class Woman
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
property :title, String, :length => 2..20
property :email, String, :nullable => false,
:format => :email_address,
:unique => true
end
27
48. DataMapper Named Scope
class Shoe
include DataMapper::Resource
property :id, Serial
property :brand, String
property :price, Float
property :available, Boolean
def self.available
all(:available => true)
end
def self.cheaper(max_price)
all(:price.lt => max_price)
end
end
Shoe.available.cheaper(210.0)
48
50. ActiveRecord Associations
class Woman
has_many :shoes
end
class Shoe
belongs_to :woman
has_many :categories, :through => :categorisations
end
50
51. DataMapper Associations
class Woman
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :shoes
end
class Shoe
include DataMapper::Resource
property :id, Serial
property :brand, String
property :price, Float
property :woman_id, Integer
belongs_to :woman
has n,:categories, :through => :categorisations
end
51
53. ActiveRecord Timestamps
# Neue Tabelle erstellen mit einer Migration
class CreateShoes < ActiveRecord::Migration
def self.up
create_table :shoes do |t|
t.string :brand
t.float :price
t.timestamps
end
end
def self.down
drop_table :shoes
end
end
# Model
class Shoe < ActiveRecord::Base
end
my_shoe = Shoe.create(:brand => "demo", :price => 99.9)
puts my_shoe.created_at
puts my_shoe.updated_at
53
54. DataMapper Timestamps
require 'initdb'
class Shoe
include DataMapper::Resource
property :id, Serial
property :brand, String
property :price, Float
property :created_at, DateTime
property :updated_at, DateTime
end
DataMapper.auto_migrate!
my_shoe = Shoe.create(:brand => "demo", :price => 99.9)
puts my_shoe.created_at
# => nil
54
60. DataMapper Multi Databases
DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/db/crm.sqlite3")
DataMapper.setup(:shop, "sqlite3:///#{Dir.pwd}/db/shop.sqlite3")
class Woman
include DataMapper::Resource
property :id, Serial
property :name, String
end
class Shoe
include DataMapper::Resource
def self.default_repository_name
:shop
end
property :id, Serial
property :brand, String
property :price, Float
end
60
62. ActiveRecord Naming Conventions
class Shoe < ActiveRecord::Base
set_table_name "tbl_Shoe"
end
class Woman < ActiveRecord::Base
set_table_name "tbl_Woman"
end
62
63. DataMapper Naming Conventions
class Shoe
include DataMapper::Resource
storage_names[:default] = 'tbl_Shoe'
#....
end
class Woman
include DataMapper::Resource
storage_names[:default] = 'tbl_Woman'
#....
end
63
64. DataMapper Naming Conventions
repository(:default).adapter.resource_naming_convention =
lambda { |klass| "tbl_#{klass.camel_case}" }
class Shoe
include DataMapper::Resource
# ....
end
class Woman
include DataMapper::Resource
#....
end
64