I'm working on a rails project that has two models - incoming_purchases and item_instances. An incoming purchase has many item_instances and an item_instance belongs to an incoming purchase. My problem is that I want to show all orders that have been received and to do this, each item_instance has to be received off the order. The item_instance model has a date_received column. So I can have an order, lets call 1.
Order 1: has two item instances where one of the items has been received:
item 1: date_received: 9/15/20
item 2: date_recieved: nil
I need to show records where ever value of date received is filled in order for the order to be received. Thus, the example above would not be received. So far, I have a scope written that looks like this:
scope :search_for_received_orders, ->{ joins(:item_instances).where.not('item_instances.date_received': [nil] ).distinct }
This would get me the example above which would be a partially filled order. My code to see NOT received orders WORKS and is this:
scope :search_for_not_received_orders, ->{ joins(:item_instances).where('item_instances.date_received': [nil, ""] ).distinct }
Basically, I need to ensure for a received order that ALL Item Instances have a date in the date_received column. I cannot figure this out. Thank you for any help.
Error I'm getting with first response below:
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:23: syntax error, unexpected '}', expecting => ...nces: {!date_received.present?})} ... ^
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:36: syntax error, unexpected do (for lambda) item_instances.each do |i| ^~
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:40: syntax error, unexpected end, expecting '}' end ^~~
scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {!date_received.present?})}
Try this:
scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {date_received: nil})}
scope :not_received_orders, -> { includes(:item_instances).where(item_instances: {date_received: nil})}
Received orders don't have any item instance with date_received nil.
Not received orders have at least 1 item instance with date_received nil
Try following, also ref this for how to use where condition
scope :search_for_received_orders,
->{ joins(:item_instances)
.where.not('item_instances.date_received': nil).distinct }
scope :search_for_not_received_orders,
->{ joins(:item_instances)
.where("item_instances.date_received IS NULL OR item_instances.date_received = ''"}).distinct }
Related
I have a json object like so:
{
_id: "12345",
identifier: [
{
value: "1",
system: "system1",
text: "text!"
},
{
value: "2",
system: "system1"
}
]
}
How can I use the XDevAPI SearchConditionStr to look for the specific combination of value + system in the identifier array? Something like this, but this doesn't seem to work...
collection.find("'${identifier.value}' IN identifier[*].value && '${identifier.system} IN identifier[*].system")
By using the IN operator, what happens underneath the covers is basically a call to JSON_CONTAINS().
So, if you call:
collection.find(":v IN identifier[*].value && :s IN identifier[*].system")
.bind('v', '1')
.bind('s', 'system1')
.execute()
What gets executed, in the end, is (simplified):
JSON_CONTAINS('["1", "2"]', '"2"') AND JSON_CONTAINS('["system1", "system1"]', '"system1"')
In this case, both those conditions are true, and the document will be returned.
The atomic unit is the document (not a slice of that document). So, in your case, regardless of the value of value and/or system, you are still looking for the same document (the one whose _id is '12345'). Using such a statement, the document is either returned if all search values are part of it, and it is not returned if one is not.
For instance, the following would not yield any results:
collection.find(":v IN identifier[*].value && :s IN identifier[*].system")
.bind('v', '1')
.bind('s', 'system2')
.execute()
EDIT: Potential workaround
I don't think using the CRUD API will allow to perform this kind of "cherry-picking", but you can always use SQL. In that case, one strategy that comes to mind is to use JSON_SEARCH() for retrieving an array of paths corresponding to each value in the scope of identifier[*].value and identifier[*].system i.e. the array indexes and use JSON_OVERLAPS() to ensure they are equal.
session.sql(`select * from collection WHERE json_overlaps(json_search(json_extract(doc, '$.identifier[*].value'), 'all', ?), json_search(json_extract(doc, '$.identifier[*].system'), 'all', ?))`)
.bind('2', 'system1')
.execute()
In this case, the result set will only include documents where the identifier array contains at least one JSON object element where value is equal to '2' and system is equal to system1. The filter is effectively applied over individual array items and not in aggregate, like on a basic IN operation.
Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js
I've got an object like this one:
{
"content": {
"iteration_size": 1
}
}
I need a JSONPath which returns null if the value of next_item is exactly 1, or the value of iteration_size otherwise. So the above should return null and "iteration_size": 2 should return 2. Is this possible in JSONPath?
I've tried variations on $.content[?(#.iteration_size == 1)].iteration_size, but the two online evaluators I've tried even disagree on the results of several of my attempts.
The use case is the AWS Batch API, where the array size must be either null or a number between 2 and 10,000.
AWS Support suggested that this was not possible, and that I should instead use a branch in the step function. I ended up replacing this:
.next(batch_submit_job)
[…]
with this:
.next(
aws_stepfunctions.Choice(self, "check_maybe_array")
.when(
aws_stepfunctions.Condition.number_equals("$.content.iteration_size", 1),
single_batch_submit_job,
)
.otherwise(array_batch_submit_job)
.afterwards()
)
where only array_batch_submit_job has an array_size. Note how the rest of the step function ended up wrapped inside the last next() call rather than at the top level.
I'm tryung to create this predicate in prolog:
The predicate json_get/3 can be defined as: json_get(JSON_obj, Fields, Result). which is true when Result is recoverable by following
the chain of fields in Fields (a list) starting from JSON_obj. A field
represented by N (with N a major number o equal to 0) corresponds to
an index of a JSON array.
Please help me to understand to follow the chain of fields.
Thanks
edit1:
Of course, so json object looks like this '{"name" : "Aretha", "surname" : "Franklin"}'.
if i call json_parse predicate to this object prolog show me this
json_obj([(”name”, ”Aretha”), (”surname”, ”Franklin”)]), we call this obj O.
with json_get i need to extract from O the name in this way, json_get(O, ["name"], R)
edit2:
with someone's help this is the predicate now:
json_get(json_obj(JSON_obj), Field, Result) :-
memberchk((Field,Result), JSON_obj).
json_get(JSON_obj, Fields, Result) :-
maplist(json_get(JSON_obj), Fields, Result).
so now the problem is nested list.
For example with this input
json_parse('{"nome" : "Zaphod",
"heads" : ["Head1", "Head2"]}', Z),
json_get(Z, ["heads", 1], R).
the output will should be R = "Head2" but the predicate doesn't extract the field and fail.
edit3:
this is the output of json_parse
json_obj([("nome", "Zaphod"), ("heads", json_array(["Head1", "Head2"]))]).
How about this
json_get(json_obj(Obj),[F|Fs],Res) :-
member((F,R),Obj),
json_get(R,Fs,Res).
json_get(json_array(Is),[N|Fs],Res) :-
nth1(N,Is,R),
json_get(R,Fs,Res).
json_get(Res,[],Res).
This produces Head1 not Head2 in your 2nd example. Please explain how that is supposed to work, if you did not just make a typo. (If it is zero-based you can just change nth1/3 to nth0/3.)
I am using parasoft SOATest to test a service response and I got a failure
Message: DataSource: products (row 1): Value Assertion: For element "../item", expected: abc but was: bcd
My Requirement is to validate the following response.
{
"samples" : {
"prds" : [
"abc",
"bcd"
]
}
}
And I have a datasource table which is like follows. First row as the column name.
prds
abc
bcd
In the SOATest I have a JSON Assertor and inside JSON Assertor I have configured a Value Assertion. In the Value Assertion I selected the first item and then in the next step I selected Apply to all "item[*]". Then Finish.
In the Expected Value I select Parameterized and select the prds from the drop down menu.
After all when the service return the above payload it failed with the above given message.
Is this a bug/limitation of SOATest or am I missing some step in here.
I believe this is just because you opted Apply to all "item[*]" instead of Apply to "item[1]" Only
I have created a LinkedHashMap, which produces a list of statuses in groovy.
These are the results of a mysql query. I want to filter the results of my map to use key entries (statusName) in the map which start with the letter "O". However I am struggling to find how to do this with a map.
My code is as follows:
db.eachRow(reportQuery, [date]) {
cache.put(it.statusName, it.statusTickets)
}
cache.each{statusName, statusTickets ->
reportMetricsWithValues[statusName] = statusTickets
table.addData([metric:statusName, value: statusTickets])
}
This is the part of my code where I need to add this filter. The code is adding the key value metrics to a database table.
To filter, you would use findAll. Pass it the map element by element and check, if the key of the element starts with the letter O; Something along the lines of:
groovy:000> [zomg: 1, omg: 2].findAll{ it.key.startsWith('o' ) }
===> [omg:2]
If you also need the "others", then groupBy (same syntax like above) could prove useful.