使用Ohm & Redis时,集合和集合或列表有什么不同?
有几个欧姆示例使用列表而不是集合(参见list doc itself):
class Post < Ohm::Model
list :comments, Comment
end
class Comment < Ohm::Model
end这种设计选择的理由是什么?
发布于 2011-01-24 23:27:56
元素的列表排序列表。当您请求整个列表时,您将按照将项目放入列表的方式对其进行排序。
集合-元素的无序集合。当您请求集合时,项目可能会以随机顺序(例如无序).**出现
在您的示例中,注释是排序的。
**我知道随机与无序不同,但它确实说明了这一点。
发布于 2011-06-09 00:08:04
只是在Ariejan的答案上进行扩展。
本质上,集合和引用是处理关联的方便方法。所以这就是:
class Post < Ohm::Model
attribute :title
attribute :body
collection :comments, Comment
end
class Comment < Ohm::Model
attribute :body
reference :post, Post
end是以下各项的快捷方式:
class Post < Ohm::Model
attribute :title
attribute :body
def comments
Comment.find(:post_id => self.id)
end
end
class Comment < Ohm::Model
attribute :body
attribute :post_id
index :post_id
def post=(post)
self.post_id = post.id
end
def post
Post[post_id]
end
end为了回答您最初关于设计选择的基本原理的问题,引入了集合和引用来提供表示关联的简单api。
https://stackoverflow.com/questions/4781605
复制相似问题