How to Create Two Foreign Keys in One Table? - ruby-on-rails

I'd like to build a Rails app to compare distances. Based on a comment about having a Distance table contain two foreign keys to Location, I've created the example below.
The Locations table will keep a record of the location name. The Distance table will keep two locations and a distance between them.
# Locations
| id | name |
| :----- | :----------- |
| 1 | location_1 |
| 2 | location_2 |
| 3 | location_3 |
# Distance
| id | location_id | location_id | distance |
| :----- | :----------- | :----------- | :------- |
| 1 | 1 | 2 | 6 |
| 2 | 1 | 3 | 13 |
How do I create two foreign keys in one table? By running rails generate model Distance distance:integer location:references I only get one location_id.

Related

how to create relationship using cypher

I have been learning neo4j/cypher for the last week. I have finally been able to upload two csv files and create a relationship,"captured". However, I am not fully confident in my understanding of the code as I was following the tutorial on the neo4j site. Could you please help me confirm what I did is correct.
I have two csv files, a "cap.csv" and a "survey.csv". The survey table contains data of each unique survey conducted at the survey sites. the cap table contains data of each unique organisms captured. In the cap table I have a foreign key, "survey_id", which in the Postgres db you would join to the p.key in the survey table.
I want to create a relationship, "captured", showing each unique organsism that was captured based on the "date" column in the survey table.
Survey table
| lake_id | date |survey_id | duration |
| -------- | -------------- | --| --
| 1 | 05/27/14 |1 | 7 |
| 2 | 03/28/13 | 2|10 |
| 2 | 06/29/19 | 3|23 |
| 3 | 08/21/21 | 4|54 |
| 1 | 07/23/18 | 5|23 |
| 2 | 07/22/23 | 6|12 |
Capture table
| cap_id | species |capture_life_stage | weight | survey_id |
| -------- | -------------- | --| -----|---|
| 1 | a |adult | 10 | 1|
| 2 | a | adult|10 | 2 |
| 3 | b | juv|23 | 3 |
| 4 | a | adult|54 | 4 |
| 5 | b | juv|23 | 5 |
| 6 | c | juv |12 | 6 |
LOAD CSV WITH HEADERS FROM 'file:///cap.csv' AS row
WITH
row.id as id,
row.species as species,
row.capture_life_stage as capture_life_stage,
toInteger(row.weight) as weight,
row.survey_id as survey_id
MATCH (c:cap {id: id})
MERGE (s) - [rel:captured {survey_id: survey_id}] ->(c)
return count(rel)
I am struggling to understand the code I wrote above. I followed the neo4j tutorial exactly but used my data (https://neo4j.com/developer/desktop-csv-import/).
I am fairly confident from data checks, but did the above code create the "captured" relationship showing each unique organism captured on that unique survey date? Based on the visual I can see I believe it did but I don't fully understand each step in the code.
What is the purpose of the MATCH (c:cap {id: id}) in the code?
The code below
MATCH (c:cap {id: id})
is the same as
MATCH (c:cap)
Where c.id = id
It is a shorter way of finding Captured node based on id and then you are creating a relationship with Survey node.
Question: s is not defined in your query. Where is it?

How to select all columns from a join table in vapor 4?

It seems that vapor add a new feature eagerLoad and remove alsoDecode. It is convenient for those which possessing parent-child or sibling relationships. But not for those without relationship.
I want to implement a tree structure whose nodes cannot be (or I don't know how to) involved in a relationship. Nodes have a parent and many children which are nodes too.
So I have three tables for this structure.
Tree:
| Field | Type |
| ----------- | --------------- |
| id | UUID? |
| name | String |
| nodes | [Node] |
| paths | [Path] |
Nodes:
| Field | Type |
| ------------- | -------------------------- |
| id | UUID? |
| type | NodeType(root, leaf, node) |
| tree | Tree |
Path:
| Field | Type |
| ------------ | --------- |
| id | UUID? |
| distance | Int |
| ancestorID | UUID |
| descendantID | UUID |
| tree | Tree |
The question is
if I want to do
SELECT Nodes.id, Nodes.type, Path.ancestorID from Nodes
INNER JOIN Path
ON Nodes.id = Path.descendantID
How to write the codes.
You could also choose to cast to SQLDatabase.
Make sure to import SQLKit too, a Fluent database can always be casted as SQLDatabase.
For example: let sqlDb = req.db as? SQLDatabase would give you the power to use custom queries like: sqlDb?.select().from("Nodes").join("Path", on: "Nodes.id = Path.descendantId").all()
For more info on SQLKit see: https://github.com/vapor/sql-kit
Reference: https://docs.vapor.codes/4.0/fluent/advanced/ (Any Fluent Database can be cast to a SQLDatabase. This includes req.db, app.db, the database passed to Migration, etc.)

Automated way to create a confusion matrix in Google Sheets?

I have a table of this form in Google Sheets:
+---------+------------+--------+
| item_id | prediction | actual |
+---------+------------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 0 |
| 4 | 0 | 1 |
| 5 | 0 | 0 |
| 6 | 1 | 1 |
+---------+------------+--------+
And I'd like to know if there's an automated way to get this kind of summary, with the counts of items that fit the criteria specified in that row/column combination:
+----------+--------------+--------------+-------+
| | prediction=0 | prediction=1 | total |
+----------+--------------+--------------+-------+
| actual=0 | 1 | 1 | 2 |
| actual=1 | 1 | 3 | 4 |
+----------+--------------+--------------+-------+
| total | 2 | 4 | |
+----------+--------------+--------------+-------+
I've been doing this somewhat manually in Google Sheets by using COUNTIFS, but I'm wondering if there's a built-in way? I tried using pivot tables, but couldn't figure out how to get the calculated fields to show the data I want.
A coworker figured it out - you can get this by creating a pivot table with the correct columns and rows, and setting the value to item_id summarized by COUNTUNIQUE.

Rails Many to many relationships with connecting or cloning two table with references?

I'm a new with Rails and I'm having trouble with some types of associations that seem a bit more complex than the ones I've been exposed to so far.
Zombie_users Body_parts_status Body_parts
| id | name | | id | user_id | body_part_id | recovery | | id | name |
|-----------| --> |----------------------------------------| --> |---------------|
| 1 | Joe | | 1 | 1 | 2 | 10% | | 1 | left leg |
| 2 | Max | | 2 | 1 | 3 | 43% | | 2 | brain |
| 3 | hair |
| 4 | blue eye |
Zobmie_users Recovery_tools Body_parts_impacts
| id | name | | id |user_id| name | | id|recovery_tool_id| body_part_id | impact |
|-----------|-->|-------------------|-->|--------------------------------------------|
| 1 | Joe | | 1 | 1 |hammer| | 1 | 1 | 2 | 10% |
| 2 | Max | | 2 | 1 |magic | | 2 | 2 | 3 | 43% |
graphic illustration of the needed functionality
We have users and a list of body parts.
I need that the users will be able to create recovery tools with which they can through Body Parts impact recover their body parts status :)
and be able to check what part of the body still need to be fixed(compared to the list) and what body parts they have already corrected.
My problem is that I do not know how to implement such connections.
because I need to have some kind of clone of the body parts to body parts status for each user.
But how I reference it so it also works with Body Parts impacts
I do not have even a concept of where to start :)
body parts table is just a long listing of all the parts of the human body
and each user should have their own "copy" of all these parts.

How do you SUM two fields from two tables, even when the field in the second table could be null?

I have the following tables:
products.rb
# has_many :sales
+----+----------+----------+-------+
| id | name | quantity | price |
+----+----------+----------+-------+
| 1 | Pencil | 30 | 1.0 |
| 2 | Pen | 50 | 1.5 |
| 3 | Notebook | 100 | 2.0 |
+----+----------+----------+-------+
sales.rb
# belongs_to :product
+----+----------+------------+
| id | quantity | product_id |
+----+----------+------------+
| 1 | 10 | 1 |
| 2 | 2 | 1 |
| 3 | 5 | 1 |
| 4 | 2 | 2 |
| 5 | 10 | 2 |
+----+----------+------------+
I'd like to know, first, how many items I have left, regardless of their type. The answer is of course 151, but that'd be cheating. I could simply make a SUM of both tables individually, then put them together to know the final number, but I'm wondering if this could be done via activerecord in a single command.
I tried the following:
Product.includes(:sales).group('products.id').sum('products.quantity - sales.quantity')
but I get:
=> {1=>73, 2=>88, 3=>0}
which is understandable, as it is going through each one to do the sum like this:
+-------------------+----------------+-----+
| products.quantity | sales.quantity | sum |
+-------------------+----------------+-----+
| 30 | 10 | 20 |
| 30 | 2 | 28 |
| 30 | 5 | 25 |
+-------------------+----------------+-----+
which equals 73.
Anyway, how could this be achieved with ActiveRecord? I want to know the total number of items, but I'd also like to know the total of each type.
I'm not familiar of any ActiveRecord way to achieve what you want but you can try mixing a little sql in there
Product
.group('products.id')
.sum('products.quantity - (SELECT SUM(sales.quantity) AS sales_quantity FROM sales WHERE sales.product_id = products.id)')

Resources