Tob*_*ede 5 ruby ruby-on-rails ohm redis
使用Ohm和Redis时,集合与集合或列表之间有什么区别?
几个欧姆示例使用列表而不是集合(请参阅列表文档本身):
class Post < Ohm::Model
list :comments, Comment
end
class Comment < Ohm::Model
end
Run Code Online (Sandbox Code Playgroud)
这种设计选择的理由是什么?
Bar*_*cha 14
只是为了扩展Ariejan的答案.
清单 - 已订购.类似于Ruby中的Array.用于排队和保持订购的物品.
设置 - 无序列表.它的行为类似于Ruby中的Array,但针对更快的查找进行了优化.
集合 - 与引用结合使用,它提供了一种表示关联的简单方法.
本质上,集合和引用是处理关联的便捷方法.所以这:
class Post < Ohm::Model
attribute :title
attribute :body
collection :comments, Comment
end
class Comment < Ohm::Model
attribute :body
reference :post, Post
end
Run Code Online (Sandbox Code Playgroud)
是以下的快捷方式:
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
Run Code Online (Sandbox Code Playgroud)
回答关于设计选择的基本原理的原始问题 - 引入了集合和引用,以提供表示关联的简单api.
列表 - 有序的元素列表.当您请求整个列表时,您将按照将它们放入列表的方式获得订购的项目.
集合 - 无序的元素集合.当您请求收集时,项目可能以随机顺序出现(例如,无序).**
在您的示例中,已订购注释.
**我知道随机与无序相同,但它确实说明了这一点.
归档时间: |
|
查看次数: |
2561 次 |
最近记录: |