Ruby Array Item Extract Specific Values - ruby

I have an array that includes a list of items that look like the following:
#<InvTypes typeID: 235, typeName: "That's the right stuff">, #<InvTypes typeID: 5, typeName: "And-some-of This">
How would I be able to extract the typeID and typeName?
I'm assuming I'd array.each, but I'm unsure from there what I'd do.
I'm .to_a the result. Without that though, the code looks like the following: #<ActiveRecord::Relation [#<InvTypes typeID: 18, typeName: "Stuff">, #<InvTypes typeID 19, typeName: "More Stuff's">, ...]>

Pluck returns an Array of attribute values type-casted to match the
plucked column names, if they can be deduced. Plucking an SQL fragment
returns String values by default.
http://apidock.com/rails/ActiveRecord/Calculations/pluck
SOLUTION:
your_relation.pluck(:typeID, :typeName)

Related

Incorrect sort of DB query results in micronaut project

I am passing this to the repository function:
Pageable.from(offset, limit).order(Sort.Order(orderBy, direction, true))
It works just fine if orderBy is the name of a String property. If I try to sort by a number property it will return the results ordered as:
1,
11,
12,
...
19,
2,
20,
21
Any suggestions on how to fix for the number ordering?
Doing the sort on the method name (such as findAllOrderByNameAsc) is not an option because this is a parameter that comes from the API. We need to support ordering by any property.
remove the ignoreCase=true parameter or pass it as false
Sort.Order(orderBy, direction, true)
you should use ignoreCase only on String fields

Laravel / Eloquent - how to ORDER BY FIELD

I'm trying to sort my query results based on a series of slash-separated ids (from the URL, e.g. /10/25/1). I grab the URL segments and explode them to get an array with elements 10, 25, 1. Now, I want my SQL to be something like this:
ORDER BY FIELD(`products`.`id`, 10, 25, 1)
In Eloquent, I'm trying to do this:
->orderByRaw('FIELD(`products`.`id`, ?)', [implode(', ', $product_ids)])
But that outputs something like this:
ORDER BY FIELD(`products`.`id`, "10, 25, 1")
I could declare a variable, loop through my array, and build the string 10, 25, 1 and just use that variable in place of the ? -- however, I would not be getting the benefit of whatever Eloquent does to prevent SQL injection attacks.
Suggestions?
Each parameter needs to have its own placeholder.
You can implode placeholders and pass the array as a parameter:
$placeholders = implode(',', array_fill(0, count($product_ids), '?'));
->orderByRaw("FIELD(`products`.`id`, $placeholders)", $product_ids)

how to correctly render referenced entities in list when I have objects instead of numberic ids?

right now in order for the list to render properly I need to have this kind of data passed in:
row = {
id: value,
name: value,
height: value,
categories: [1,2,3,4]
}
how can I adapt the code so that a list works with this kind of data?
row = {
id: value,
name: value,
height: value,
categories: [{id: "1"},{id: "2"},{id: "3"},{id: "4"}]
}
when I try to do that it seems that it applies JSON.stringify to the objects so it is trying to find category with id [Object object]
I would to avoid a per case conversion of data as I do now..
it seems that I cannot do anything in my restClient since the stringify was already applied
I have the same issue when I fetch just one data row e.g in Edit or Create.. categories ReferenceArrayInput is not populated when categories contains objects
Have you tried using format?
https://marmelab.com/admin-on-rest/Inputs.html#transforming-input-value-tofrom-record
Might help transform your input value. Then you can use format() to change values back to the format your API expects.
If this does not work then you will have to probably create a custom component out of ReferenceArrayInput.

rails 5 enum where "like"

I'm trying to query an activerecord model enum and use the like operator in the where method, and it just doesnt work. Is there some trick to allow me to query an enum this way? Works fine on regular columns. Here it is in the console.
Regular string column (title) works as shown below
irb(main):092:0> Proposal.select(:id,:department,:status).where('title like "test%"')
Proposal Load (0.3ms) SELECT "proposals"."id", "proposals"."department", "proposals"."status" FROM "proposals" WHERE (title like "test%") LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Proposal id: 7, department: 1, status: "In Progress">, #<Proposal id: 61, department: 2, status: "Won">]>
However, trying it on an enum, gives no results.
irb(main):094:0> Proposal.select(:department,:status).where('status like "Wo%"')
Proposal Load (0.3ms) SELECT "proposals"."department", "proposals"."status" FROM "proposals" WHERE (status like "Wo%") LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation []>
Any idea why I can't use like operator on enum? I'm trying to use this to filter a view with datatables.net server side processing.
Enum stores data as integer like 0,1,2,3... Then rails map number to enum value defined in model. That is the reason why you doesn't get result

method naming convention for sortBy orderBy with multiple arguments, best practices

For example i have table (e.g. in-memory collection) with name lastname fields and more.
Example JSON:
[
{name: 'arina', lastname: 'anyone'},
{name: 'bob', lastname: 'zorro'},
{name: 'bob', lastname: 'black'},
]
now i want a data sorting method, that sorts the data by name and lastname
that means i want the result:
arina - anyone
bob - black
bob - zorro
but if i write something like
function sortByNameAndLastname(data) {
//here i should first sort by lastname, then by name to get result i want
sortByLastName(data);
sortByName(data);
}
that is somehow confusing, if you read the order of calls.
What is the naming convention / best practices to name such method?
sortByNameAndLastname or sortByLastnameAndName ?
if you see SQL, there is a first case ORDER BY name, lastname returns the result i want.
You could be consistent with SQL syntax and use the naming convention of:
OrderBy<firstField>Then<SecondField>
For example in your case:
OrderByNameThenLastName
Your "name" field really should be renamed "firstname", especially as you have a field called "lastname" for the surname.

Resources