Retrieve items that do not have an empty array list - ruby-on-rails

I have a list of items, those items have some attributes one of which is an array list. The array list may be empty or not.
How do i retrieve the items from the list that their array list is not empty

You can use select together with present? (or reject/blank?):
items = items.select{|x| x.array_attribute.present?}

Related

OData filter objects that contain particular items in subarray

I have the following situation:
I have an object
This object has an item that is an object again
The second object contains array of objects with ids
Can I filter only the parent objects that have subobjects that contain something in particular in the array on the bottom of the tree?
Try this:
$filter=nameOfObjectArrayProperty/any(o: contains(o/id, 'some-alue'))
or
$filter=nameOfObjectArrayProperty/any(o: o/id eq 'some-value')
It's also important to note that "/any" can be replaced with "/all" if you want all the subobjects to match your filter criteria.

How to get the index based on array values

I have one array like "house". Each house object has multiple parameters like id, name, images, address. And i am showing all these in table view. Now i have one parameter key name.
I have to loop in to my house array , and find out the name which is contain in my array and i have to find out the Index.
So that in my table view i need to scroll that particular house item to top.
Any solution how can i achieve that.
Eg: Array - > [[id, name, image,address], [id, name, image,address], [id, name, image,address], [id, name, image,address]]
I have one key : "name".
I have to loop in to my array and get the index, so that i can scroll to that particular index.
Thanks
You can use the functions firstIndexOf and lastIndexOf functions available in swift. You can find details on How to find index of list item in Swift?

How to get list length?

I have a list of items and other empty list.. when I click on an item I send it to the empty list and I returns that it has an item in.. but when I called that list after adding items in it returns length = 0. what can I do?
enter image description here

Active Record Array array query - to check records that are present in an array

I have an Objective model which has an attribute called as labels whose values are array data type. I need to query all the Objectives whose labels attribute has values that are present in some particular array.
For Example:
I have an array
a = ["textile", "blazer"]
the Objective.labels may have values as ["textile, "ramen"]
I need to return all objectives that might have either "textile" or "blazer" as one of their labels array values
I tried the following:
Objective.where("labels #> ARRAY[?]::varchar[]", ["textile"])
This returns some records.Now when I try
Objective.where("labels #> ARRAY[?]::varchar[]", ["textile", "Blazer"])
I expect it to return all Objectives which contains at-least one of the labels array value as textile or blazer.
However, it returns an empty array. Any Solutions?
Try && overlap operator.
overlap (have elements in common)
Objective.where("labels && ARRAY[?]::varchar[]", ["textile", "Blazer"])
If you have many rows, a GIN index can speed it up.

sorting custom objects from array into dictionary by property

Is there an easy way to sort an array of custom objects (in this case Lists) into a dictionary based on a particular property of each list.
[List1, List2, List3, List4, List5];
For example, each List object has an NSString type property, which can be either "MyList","Sent","Received"
How would I create a dictionary based on these properties so that I have a dictionary like so:
"MyList" -> array of lists with MyList as their type property [List1, List5];
"Sent" -> array of lists with Sent as their type property "Received" [List3;
"Received" -> array of lists with Received as their type property [List2, List4];
I'd really rather not loopthrough my entire array of List objects if possible
You need to iterate the array and build your dictionary. If the key isn't already there, create the array and add it to the dictionary, then add the new item to it.
You could alternately use predicates to filter the array into sub-arrays and build the dictionary like that but it's a similar amount of code (for a few options) and doesn't support automatic future expansion when you have another value for the key you're organising on.

Resources