-
Notifications
You must be signed in to change notification settings - Fork 74
Models
Pedro Belo edited this page Aug 6, 2015
·
5 revisions
Pliny models are just like Rails', only using Sequel::Model
instead of ActiveRecord
. Use them to wrap access to database.
You should try keep models lean. Think of them as pure data objects: every single method in a model should only return data that came from the database or was generated from another static source. They should not manipulate data, interact with other models, make API requests, etc – Mediators are a much better fit for these.
To get a new model:
$ pliny-generate model todo
created model file ./lib/models/todo.rb
created migration ./db/migrate/1408995997_create_todos.rb
created test ./spec/models/todo_spec.rb
Then edit the migration to add any additional fields you might need beyond the id and timestamps:
Sequel.migration do
change do
create_table(:todos) do
uuid :id, default: Sequel.function(:uuid_generate_v4), primary_key: true
timestamptz :created_at, default: Sequel.function(:now), null: false
timestamptz :updated_at, default: Sequel.function(:now), null: false
end
end
end
Further reading:
Basics
Diving in
- bin/setup
- Config
- CORS
- Endpoints
- Error Handling
- Logging
- Models
- Mediators
- Migrations
- Rake Tasks
- Request IDs
- RequestStore
- Schema
- Serialization
- Testing
- Updating
Guides