forked from datamapper/dm-core
/
FAQ
74 lines (47 loc) · 2.24 KB
/
FAQ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
:include:QUICKLINKS
= FAQ
=== So where's my :id column?
DataMapper will NOT create an auto-incrementing <tt>:id</tt> key for you
automatically, so you'll need to either explicitly create one with
property :id, Fixnum, :serial => true
You can choose to use a natural key by doing
property :slug, String, :key => true
Remember, DataMapper supports multiple keys ("composite keys"), so if your
model has two or more keys, no big deal
property :store_id, Fixnum, :key => true
property :invoice_id, Fixnum, :key => true
=== How do I make a model paranoid?
Create a property and make it a ParanoidDateTime or ParanoidBoolean type.
property :deleted_at, ParanoidDateTime
property :deleted, ParanoidBoolean
All of your calls to <tt>##all()</tt>, <tt>##first()</tt> will be scoped
with <tt>:deleted_at => nil</tt> or <tt>:deleted => false</tt>. Plus,
you won't see deleted objects in your associations.
=== Does DataMapper do Single Table Inheritance?
This is what the Class data-type is for:
class Person
include DataMapper::Resource
property :id, Fixnum, :serial => true
property :type, Class ## other shared properties here
end
class Salesperson < Person; end
You can claim a column to have the type <tt>Class</tt> and DataMapper will
automatically drop the class name of the inherited classes into that field of
the data-store.
=== How do I run my own commands?
repository.adapter.query("select * from users where clue > 0")
repository(:integration).adapter.query("select * from users where clue > 0")
This does not return any Users (har har), but rather Struct's that will quack
like Users. They'll be read-only as well.
<tt>repository.adapter.query</tt> shouldn't be used if you aren't expecting a result set
back. If you want to just execute something against the database, use
<tt>repository.adapter.execute</tt> instead.
=== Can I get an query log of what DataMapper is issuing?
Yup, to set this up, do:
DataMapper::Logger.new(STDOUT, 0)
Incidentally, if you'd like to send a message into the DataMapper logger, do:
DataMapper.logger.debug { "something" }
DataMapper.logger.info { "something" }
DataMapper.logger.warn { "something" }
DataMapper.logger.error { "something" }
DataMapper.logger.fatal { "something" }