The Ruby equivalent of the active record. Where (criteria)

It can be a very long step, but make life easier if it existed.

Here is the script anyway. I have an array of hashes with one key. Another hash ........ Yes, I know.

Here is the best explanation:

@myArrayOfStuff[0]
@myArrayOfStuff[0]["single-key"]
@myArrayOfStuff[0]["single-key"]["object-identifier"]

The first returns a hash. The second will return an object (called a page in my case, but the example uses different names) The third returns any variable, I have a link as an identifier for the object.

Simple enough.

What I would like to do is select another array where the value of the object identifier is not nil or greater than x. Something similar to the activerecord.where method.

@x = @myArrayOfStuff.where(["single-key"]["object-identifier"]) > 3orwhatever

, , . ? - , . -

@x = @myArrayOfStuff.sort {|x,y| y <=> x } 

, ruby. - ?

+5
2

select.

@x = @my_array_of_stuff.select {|v| v["single-key"]["object-identifier"] > 3}
+14
collection = [
  {a:1, b:2, c:3},
  {a:2, b:3, c:4}
]
where = {a:1}
collection.select{|item|
  where.map{|k,v|
    item[k] == v ? true : nil
  }.compact.length == where.length
}
0

All Articles