I have a full set of items - ["A", "B", "C", "D", "E"] and for a certain entity I need to select only e subset of those. I keep this information as array for each entity items = ["A", "C", "D"].
Each item is a column in a table. What I want is by using the array to select only those columns which are in the array for the given entity.
For example if I have
object.items = ["A", "C", "D"]
I want to have this SQL query executed
SELECT A, C, D FROM table WHERE...
But I need this done with ActiveRecords. So something like:
Entity.select(columns: items)..
Of course this is just a pseudocode. I don't know how to actually do it with AR, but I think that there is a good chance to have something build in which allows to perform such a select?
You can do as below,
Entity.select(*items)
As below,
array = ['id', 'client_name']
Event.select(*array)
Event Load (1.6ms) SELECT `events`.`id`, `events`.`client_name` FROM `events`
You can do this
Entity.select(items.map(&:to_sym))
But make sure items get all correct column names.
Related
I am browsing through the ruby Array iterators. And I can't find what I am looking for and I think it already exists:
I have two arrays:
["a", "b", "c"]
[0,1,2]
And I want to merge as so:
[ [0, "a"], [1, "b"], [2, "c"] ]
I think the iterator exists in the standard library (I used it before) but I am having trouble finding its name.
This should work:
[0,1,2].zip(["a", "b", "c"]) # => [[0, "a"], [1, "b"], [2, "c"]]
From the official documentation of the Array#zip function:
Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.
This generates a sequence of ary.size n-element arrays, where n is one more than the count of arguments.
For more info and some other examples, refer to:
https://ruby-doc.org/core-2.4.2/Array.html#method-i-zip
You are looking for the zip function
https://apidock.com/ruby/Array/zip
I think you could use https://apidock.com/ruby/Enumerator/each_with_index
See this post difference between each.with_index and each_with_index in Ruby?
or if you have specific values and you want to map them you would use map or zip. It explains it well in this post
Combine two Arrays into Hash
Firebase Database sorts results lexicographically, and I have found no way to preserve this sort in Dart.
How would one sort lexicographically in Dart, or preserve the order provided by Firebase Database?
Take a look here: Sort List by alphabetical order
List<String> input = ["b", "a", "c", "aa"];
print(input.toString());
input.sort((a, b) {
return a.toLowerCase().compareTo(b.toLowerCase());
});
print(input.toString()); // [a, aa, b, c]
I have the next table in CoreData entity:
`title`
`description`
`category`
Now I want to group all records by category. For example:
("A", "B", "Cat")
("C", "D", "Dog")
("E", "F", "Cat")
I want to get them in a dictionary like: [Category: [Items]]
Where Category is struct like (title, description) and Items is my above described structure like (title, description, category).
So as a result I want to get dictionary
[Category("Cat"): [Items("A", "E")], Category("Dog"): [Items("C")]]
How can I do that in Swift?
I've tried to do with distinct but I've got wrong results there.
Based upon your example with limited info re: what your data model looks like, I would take whatever entity array you've got and create a dictionary using Dictionary(grouping:by:), as referenced in my comment.
Assuming an Core Data entity with the attributes you reference, you would create a dictionary as follows:
let dictionary = Dictionary(grouping: entityArray) { $0.category }
This assumes you've got an array of entities with a title, description, and category properties.
Lets say I have 2 arrays of users.
[user1, user2, user3]
[user3]
Based on the second array, I want to sort the first array so that occurrences in the second array appear first in the first array.
So the result of the first array would be:
[user3, user1, user2]
I realise the simple way would be to iterate over the first array and populate an empty array, ordering it if the second array contains it, then merging the rest. The below is pseudo code and untested, but gives an idea of what I was thinking as a simple solution
return_array = []
array1.each do |a|
if array2.include? a
return_array.push array1.pop(a)
end
end
return_array.merge array1
Is there any way to refine this? Built in rails or ruby methods for example.
You should use array intersection and array difference:
a&b + a-b
would give you what you're looking for.
The manual for intersection: http://ruby-doc.org/core-2.2.0/Array.html#method-i-26
The manual for difference: http://ruby-doc.org/core-2.2.0/Array.html#method-i-2D
You could just do this:
array2 + (array1 - array2)
my_array = ['user1', 'user2', 'user3']
my_array2 = ['user3']
my_array2 + (my_array.sort - my_array2)
I have a nested array in Ruby:
array = [["a", "b"], ["c", "d"]]
What command can I use to remove the nested array that contains "a" from the array?
Thanks for any help.
array.delete_if{|ary| ary.kind_of?(Array) and ary.include?('a') }
Deletes all arrays which include "a"
Do you specifically want to remove ["a", "b"], knowing that's exactly what it is, or do you want to remove any and all arrays that contain "a", no matter their remaining values? It's not clear whether you meant 'the nested array that contains "a"' as part of the problem specification, or just a way of indicating which of the elements in your specific example you wanted the answer to target.
For the first one, you can use DigitalRoss's answer.
For the second, you can use Huluk's, but it's overly specific in another way; I would avoid the kind_of? Array test. If you know the elements are all arrays, then just assume it and move on, relying on exceptions to catch any, well, exceptions:
array.delete_if { |sub| sub.include? 'a' }
If you do need to test, I would use duck-typing instead of an explicit class check:
array.delete_if { |item| item.respond_to? :include? and item.include? 'a' }
> [["a", "b"], ["c", "d"]] - [["a", "b"]]
=> [["c", "d"]]
If you don't already have a handle on the element other than knowing it contains an "a", you can do:
array - [array.find { |x| x.include? "a" }]
Try this:
array.delete_if { |x| x.include? "a" }