Ohm & Redis: when to use a collection, list or collection?

What is the difference between a collection and a collection or list when using Ohm and Redis?

Some Ohm examples use a list rather than a collection (see the doc list ):

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

What is the point of such a design choice?

+5
source share
2 answers

List - an ordered list of items. When you request the entire list, you get the ordered elements as you put them in the list.

A collection is an unordered set of elements. When you request a collection, items may appear randomly (e.g., out of order). **

In your example, the comments are ordered.

** , - , , .

+5

, Ariejan.

  • - . Ruby. .

  • Set - . Ruby, .

  • - , .

, . :

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 .

+14
source

All Articles