redis-orm 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -6
- data/lib/redis/orm/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -55,15 +55,11 @@ Like `ActiveRecord`, `redis-orm` defines `created_at` and `updated_at` attribute
|
|
55
55
|
There are 3 core relationships defined by `redis-orm`: `belongs_to`, `has_one` and `has_many`. They function essentially similar to relations of the same name in ActiveRecord, but it's important to keep in mind that they are specifically designed for Redis, and they have some minor differences.
|
56
56
|
|
57
57
|
class User < Redis::ORM
|
58
|
-
|
59
|
-
|
60
|
-
has_many :posts
|
61
|
-
has_one :profile
|
58
|
+
has_many :posts, :relation => :user
|
59
|
+
has_one :profile, :relation => :user
|
62
60
|
end
|
63
61
|
|
64
62
|
class Post < Redis::ORM
|
65
|
-
attribute :subject, "(no subject)"
|
66
|
-
attribute :body
|
67
63
|
belongs_to :user
|
68
64
|
end
|
69
65
|
|
@@ -71,6 +67,26 @@ There are 3 core relationships defined by `redis-orm`: `belongs_to`, `has_one` a
|
|
71
67
|
belongs_to :user
|
72
68
|
end
|
73
69
|
|
70
|
+
As shwon above, you should usually follow your `has_one` and `has_many` directives with a `relation` option, which matches the corresponding `belongs_to` relation in the other models. Think of it as the `foreign_key` option in ActiveRecord. This option is not strictly required and `redis-orm` will work fine without it, but you may have problems looking up the reverse relations (e.g. the `belongs_to` part) without it.
|
71
|
+
|
72
|
+
Alternatively, you can specify a `relation` option for the `belongs_to` directive, instead:
|
73
|
+
|
74
|
+
class User < Redis::ORM
|
75
|
+
has_many :posts
|
76
|
+
has_one :profile
|
77
|
+
end
|
78
|
+
|
79
|
+
class Post < Redis::ORM
|
80
|
+
belongs_to :user, :relation => :posts
|
81
|
+
end
|
82
|
+
|
83
|
+
class Profile < Redis::ORM
|
84
|
+
belongs_to :user, :relation => :profile
|
85
|
+
end
|
86
|
+
|
87
|
+
TODO: A future version of this gem will infer the `relation` option from the class name if it omitted.
|
88
|
+
|
89
|
+
|
74
90
|
#### inference
|
75
91
|
|
76
92
|
Unlike `ActiveRecord`, `redis-orm` does _not_ infer class names from the relation name. You can give any value you like to the relations. During look-up, class names are retrieved from the object's ID, which (as mentioned) is already maintained for you. So in most cases, you should not have to care about the object's class at all. The only caveat is, all related objects _must_ inherit from `Redis::ORM` so that they can be looked up and deserialized properly.
|
data/lib/redis/orm/version.rb
CHANGED