Assuming I have a model named "Person" & that person is defined by name, age, gender & race.
How do I iterate through the model to find out which values are nil?
For example:
name: peter
age: 34
gender: nil
race: nil
--Nil count: 2--
I understand I would have iterate through each field, do a +1 if value if nil & output the total value.
Thanks for any help or guidance!
If your instance is p then:
nils = p.attributes.values.select(&:nil?).count
will give you the number of nil attributes.
Related
Neo.ClientError.Statement.SyntaxError: Invalid input ')': expected
whitespace or a relationship pattern (line 66, column 100 (offset:
1898)) "CREATE (z:Subscription{ subscriptionId: subs.subscriptionId,
startDate: subs.startDate, endDate:''})<-[r:ASSOCIATION]-(y:Person
{nationalIdentityNumber: subs.nationalIdentityNumber, name: subs.name,
surname: subs.surname, fathername: subs.fathername , nationality:
subs.nationality, passportNo: subs.passportNo, birthdate:
subs.birthdate})"
I want to create/merge nodes and relation that types are Person, Subscription and Line
If I had same subscription I should check to startDate, If new data's start date greater then old data; I sould create new Subscription and also change old subscription's end date.
UNWIND [{
msisdn:'99658321564',
name:'Lady',
surname:'Camble',
fatherName:'Aeron',
nationality:'EN',
passportNo:'PN-1234224',
birthDate:'12-05-1979',
nationalIdentityNumber:'112124224',
subscriptionId:'2009201999658321564',
startDate:'20-09-2019 12:00:12'
},{msisdn:'99658363275',
name:'John',
surname:'Mckeen',
fatherName:'Frank',
nationality:'EN',
passportNo:'PN-126587',
birthDate:'15-08-1998',
nationalIdentityNumber:'2548746542',
subscriptionId:'1506201999658363275',
startDate:'15-06-2019 13:00:12'}
{
msisdn:'99658321564',
name:'Lady',
surname:'Camble',
fatherName:'Aeron',
nationality:'EN',
passportNo:'PN-1234224',
birthDate:'12-05-1979',
nationalIdentityNumber:'112124224',
subscriptionId:'2009201999658321564',
startDate:'31-11-2019 12:00:12'
}
] as subs
MERGE (y:Person {nationalIdentityNumber: subs.nationalIdentityNumber, name: subs.name, surname: subs.surname, fathername: subs.fathername , nationality: subs.nationality, passportNo: subs.passportNo, birthdate: subs.birthdate })
MERGE (t:Subscription{subscriptionId:subs.subscriptionId })
MERGE (y)-[rel:ASSOCIATION]-(t)
ON MATCH SET
t.endDate = (case when t.startDate <subs.startDate then subs.startDate else ''
end)
MATCH (t:Subscription) where t.subscriprionId=subs.subscriprionId and
(CASE
WHEN t.endDate=subs.startDate then
CREATE (z:Subscription{ subscriptionId: subs.subscriptionId, startDate: subs.startDate, endDate:''})-[r:ASSOCIATION]-(y:Person {nationalIdentityNumber: subs.nationalIdentityNumber, name: subs.name, surname: subs.surname, fathername: subs.fathername , nationality: subs.nationality, passportNo: subs.passportNo, birthdate: subs.birthdate})
END)
RETURN y
UNWIND[...] as subs
MERGE (y:Person {nationalIdentityNumber: subs.nationalIdentityNumber, name: subs.name, surname: subs.surname, fatherName: subs.fatherName , nationality: subs.nationality, passportNo: subs.passportNo, birthDate: subs.birthDate })
MERGE (t:Subscription{subscriptionId:subs.subscriptionId,startDate:subs.startDate,endDate:''})
MERGE (y)-[rel:ASSOCIATION]-(t)
MERGE(x:Subscription{subscriptionId:subs.subscriptionId, endDate:''})
SET
x.endDate = (case when x.startDate < subs.startDate then subs.startDate else null end);
CQL should like this. Thanks my co-worker.
You're trying to have conditional Cypher clauses through a CASE statement, and that won't work. You can't do a nested CREATE (or any other Cypher clause) in a CASE.
You can however use a trick with FOREACH and CASE to mimic an if conditional. That should work in your case, as you want to only execute a CREATE under certain conditions (though since you already matched to the y node for the person, just reuse (y) in that CREATE instead of trying to define the entire node again from labels and properties, that won't work properly).
If you need more advanced conditional logic, that's available via conditional procs in APOC Procedures
as the long name of this question suggests, I am trying to sort an array of objects by those object's properties, but I am having issues with the order in which the array is returned. Basically, my database structure has each date, in order, and then entries under ascending numbers under that date's directory. Below is a visual representation of the structure:
Balances
-11/7/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
-11/8/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
-11/9/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
When I retrieve the data from the database through a SingleValueEvent and add it to an array of custom objects, the data in the array for some reason gets out of order, and doesn't stay in the exact order as the data is stored in the database. To solve this problem, I tried to use the following code to order the array:
self.tableArray = self.tableArray.sorted(by: { $0.date < $1.date })
self.tableArray.sort(by: { (object1, object2) -> Bool in
if object1.date != object2.date {
return object1.date < object2.date
} else {
return object1.date < object2.date
}
})
The array returned after using either of the two lines of code is sorted by each object's date, but the order of each of the items under the date in the database is screwed up. I know this is kind of confusing, which is why I have included a visual representation below which I hope will help.
Original Array:
Item 1:
date: 11/14/17
name: Name 1
Item 2:
date: 11/13/17
name: Name 1
Item 3:
date: 11/13/17
name: Name 2
Item 4:
date: 11/7/17
name: Name 1
Sorted Array:
Item 1:
date: 11/7/17
name: Name 1
Item 2:
date: 11/13/17
name: Name 2
Item 3:
date: 11/13/17
name: Name 1
Item 4:
date: 11/14/17
name: Name 1
Thanks!
There could be a few reasons, the way it is serialised or sorted in storage - without specific insight into if you're using CoreData, Real, SQLite or other...
TL; DR, if you want to ensure the order is exactly as per what you want, and it looks like you have multiple sorting criteria, define that criteria as an attribute. You cannot always guarantee persistence will maintain the ordering, so build it into the schema.
I.e.
{name, date}
Add an extra attribute
{name, date, order}.
The order property can be globally unique or just unique within items of that date.
You can either sort twice (with the second sort being stable), or better yet define a comparison that first compares by date, and then by the other.
self.tableArray.sort(by: { (object1, object2) -> Bool in
if object1.date != object2.date {
return object1.date < object2.date
} else {
return object1.order < object2.order
}
})
Setup: Rails + Postgres.
I have table A with columns
id: int, name: string, address: string, s_array: varchar[], i_array: int[], created_at: datetime)
For a row, i need to find all the rows which have similar values.
In rails, then query would look like
row = A.find(1) # any random row
ignore_columns = %w[id created_at]
A.where(row.attributes.except(*ignore_columns))
This works if we don't have column with array type.
How to find all records where a value = [given array]?
Edit:
To be clear, I want to pass multiple columns in where clause where some columns are of type array. For values in where clause, I am passing hash (row.attributes.except(*ignore_columns) is a hash)
Edit 2: Example:
Lets say I have a Query table
Query(id: Int, name: String, terms: varchar[], filters: int[], city: string, created_at: datetime)
id = primary key/integer
terms = array of string
filters = array of integer (it is an enum and we can select multiple which is saved as array)
other fields = self explanatory
Suppose I have following rows
(1, "query1", ["john"], [0], "wall", <some_date>)
(1, "query2", ["eddard", "arya"], [0, 1], "Winterfell", <some_date>)
(1, "query3", ["sansa", "arya"], [1, 2], "Winterfell", <some_date>)
Now when I add new row
row = ActiveRecord of (1, "query4", ["eddard", "arya"], [0, 1], "Winterfell", <some_date>)
What I want is to search already existing records like this
ignore_attributes = %w[id name created_at]
Query.where(row.attributes.except(*ignore_attributes))
This query should return already existing query3 so that I won't need to add new row with name query4.
The problem is that because some column types are of array type, then passing them as hash/conditions in where clause is not working.
use find_by to find_all_by and it will return all matching results.
Try this example:-
##black list of attributes
ignore_attributes = %w[id name created_at]
MyModel.where(active: true).select(MyModel.attribute_names - ignore_attributes)
=====The above query can also be chained as:- =====
##you have a city column too
MyModel.where(active: true).select(MyModel.attribute_names - ignore_attributes).where.not(:city=>["Sydney","London"])
If you need this a permanent fix,you can add this line in your model.rb file.
but its dangerous.
self.ignored_columns = %w(id name created_at)
Hope it helps :)
Your query in rails look like below
row = A.find(1)
where_clauses = row.attributes.reject{|k,v| %[id, created_at].include?(k)}
A.where(where_clauses)
I want a table like the following:
local Users = {}
local function GetUsers (user)
--cycle through all Users
local Id = GetUserID (user)
local Age = GetAge (user)
local Type = GetType (user)
--Id returns ID of User (value about 8 char length: 27296654)
table.insert (Users, {[Id] = {Age = Age, Type = Type}}
end
This is working as it should but
#Users == 0
if I call Users[Id].Age it returns correct value.
How to make the # work?
As I want to cycle trough all Users to check if a User is multiple times in or missing.
They need to be sorted via IDS.
I also thought on transforming the IDs to word with string.char()
As words will be counted as I want it to be.
I want to make it with
for i = 1, #Users do
An example table looks like this:
Users = {
[12345678] = {Age = 18, Type = 1}
[62952766] = {Age = 22, Type = 1}
[23456788] = {Age = 33, Type = 1}
}
So #Users have to be 3 for me but it shows 0.
But I can call the Age and type out of the table, that means they are in.
Does the table have Problems when the index is such a high number?
Look at # operator documentation:
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
You are using dictionary, so the # operator doesn't work as you expect it. The only way is to iterate the whole table with pairs(..).
function getTableLength(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
Person domain class is as follows:
String name
String gender
boolean available
double salary
What i want to do is to query the database to find out all the Person's who are available and increase their salary by 100.
def person = new Person()
// now i need to get the count of available person
I think i have to use Person.executeQuery("THE QUERY"), but i don't know how to proceed.
I think you could do something like this:
Person.executeUpdate('update Person p set p.salary = p.salary+100 where p.available = :available', [available: true])
Or:
Person.executeUpdate('update Person p set p.salary = p.salary+100 where p.available = TRUE')
I hope that helps.