Class | Ohm::Set |
In: |
lib/ohm.rb
|
Parent: | Object |
key | [R] | |
model | [R] | |
namespace | [R] |
Retrieve a specific element using an ID from this set.
Example:
# Let's say we got the ID 1 from a request parameter. id = 1 # Retrieve the post if it's included in the user's posts. post = user.posts[id]
Reduce the set using any number of filters.
Example:
set = User.find(:name => "John") set.except(:country => "US") # You can also do it in one line. User.find(:name => "John").except(:country => "US")
Returns true if id is included in the set. Otherwise, returns false.
Example:
class Post < Ohm::Model end class User < Ohm::Model set :posts, :Post end user = User.create post = Post.create user.posts.add(post) user.posts.exists?('nonexistent') # => false user.posts.exists?(post.id) # => true
Returns the first record of the set. Internally uses `sort` or `sort_by` if a `:by` option is given. Accepts all options supported by `sort`.
class User < Ohm::Model attribute :name end User.create(name: "alice") User.create(name: "bob") User.create(name: "eve") User.all.first.name # => "alice" User.all.first(by: :name).name # => "alice" User.all.first(order: "ASC") # => "alice" User.all.first(order: "DESC") # => "eve"
You can use the `:order` option to bring the last record:
User.all.first(order: "DESC").name # => "eve" User.all.first(by: :name, order: "ALPHA DESC") # => "eve"
Returns an array with all the ID‘s of the set.
class Post < Ohm::Model end class User < Ohm::Model attribute :name index :name set :posts, :Post end User.create(name: "John") User.create(name: "Jane") User.all.ids # => ["1", "2"] User.find(name: "John").union(name: "Jane").ids # => ["1", "2"]
Check if a model is included in this set.
Example:
u = User.create User.all.include?(u) # => true
Note: Ohm simply checks that the model‘s ID is included in the set. It doesn‘t do any form of type checking.
Allows you to sort your models using their IDs. This is much faster than `sort_by`. If you simply want to get records in ascending or descending order, then this is the best method to do that.
Example:
class User < Ohm::Model attribute :name end User.create(:name => "John") User.create(:name => "Jane") User.all.sort.map(&:id) == ["1", "2"] # => true User.all.sort(:order => "ASC").map(&:id) == ["1", "2"] # => true User.all.sort(:order => "DESC").map(&:id) == ["2", "1"] # => true
Allows you to sort by any attribute in the hash, this doesn‘t include the id. If you want to sort by ID, use sort.
class User < Ohm::Model attribute :name end User.all.sort_by(:name, :order => "ALPHA") User.all.sort_by(:name, :order => "ALPHA DESC") User.all.sort_by(:name, :order => "ALPHA DESC", :limit => [0, 10])
Note: This is slower compared to just doing `sort`, specifically because Redis has to read each individual hash in order to sort them.
Do a union to the existing set using any number of filters.
Example:
set = User.find(:name => "John") set.union(:name => "Jane") # You can also do it in one line. User.find(:name => "John").union(:name => "Jane")