In products table I have fields like
id product_name product_value quantity status
1 abc 10000 50 received
2 efg 5000 15 shipment
3 hij 850 100 received
4 klm 7000 20 shipment
5 nop 350 50 received
I can select multiple rows at a time. And here I selected id=2,4 and need to change the status='received'. How to do multiple update at single time in rails?
Try
Product.where(id: [2, 4]).update_all(status: 'received')
If you are looking for all products that have a status of 'shipment', you can use:
Product.where(status: 'shipment')
From here, you can set all to 'received', or iterate through them and select only the ones you want to make changes to.
Related
-I'm trying to create a google sheet that tracks progress of players in a team.
-Each season has its own tab.
-A tab that combines data from all sheets is needed.
-The players in the seasonal tabs are going to be in different order each season based on their performance, there are also going to be players coming and going so I can't know which row is going to include which player's data.
-The combined table needs to be able to refer to rows in other tabs where the player name is present, and count up total points for them.
Picture 1 is individual season table. Picture 2 is where I was stuck. Picture 3 is the desired output, which was achieved using the answer provided.
Example of desired output:
Player
Season 17
Season 18
Total
Player1
1800
880
1680
Player4
100
100
200
Player12
50
21
71
Player2
33
11
44
Player3
2
5
7
Player33
1
2
3
You can merge all the data in a new sheet by using the following formula:
={FILTER(Sheet1!$B$3:$P, len(Sheet1!$B$3:$P)),
FILTER(Sheet2!$B$3:$P, len(Sheet1!$B$3:$P)),
FILTER(SheetN!$B$3:$P, len(Sheet1!$B$3:$P))}`
You have to repeat the FILTER(...) part for each sheet.
In the new sheet, get the list of unique user names with =UNIQUE($A:$A), where A is the column with the player names. You can also use a list you already have instead.
You can then use =SUMIF($A:$A;P1;$B:$B) to compute the grand total for each player, where A is the column containing all the names, B the column containing all the total scores, and P the column containing the UNIQUE player names.
Alternatively, you can use =VLOOKUP(P1, SheetN!$B$3:$N$100, 13, FALSE) in the new sheet to read the total score of the player with the name in the cell P1.
I think that with more advanced magic you may be able to automatically gather the data from all the sheets, but in this case you have to manually type each heet name.
Let's say, for simplicity sake, I have the following table:
id amount p_id date
------------------------------------------------
1 5 1 2020-01-01T01:00:00
2 10 1 2020-01-01T01:10:00
3 15 2 2020-01-01T01:20:00
4 10 3 2020-01-01T03:30:00
5 10 4 2020-01-01T03:50:00
6 20 1 2020-01-01T03:40:00
Here's a sample response I want:
{
"2020-01-01T01:00:00": 25, -- this is from adding records with ids: 2 and 3
"2020-01-01T03:00:00": 55 -- this is from adding records with ids: 3,4,5 and 6
}
I want to get the total (sum(amount)) of all unique p_id's grouped by the hour.
The row chosen per p_id is the one with the latest date. So for example, the first value in the response above doesn't include id 1 because the record with id 2 has the same p_id and the date on that row is later.
The one tricky thing is I want to include the summation of all the amount per p_id if their date is before the hour presented. So for example, in the second value of the response (with key "2020-01-01T03:00:00"), even though id 3 has a timestamp in a different hour, it's the latest for that p_id 2 and therefore gets included in the sum for "2020-01-01T03:00:00". But the row with id 6 overrides id 2 with the same p_id 1.
In other words: always take the latest amount for each p_id so far, and compute the sum for every distinct hour found in the table.
Create a CTE that includes row_number() over (partition by p_id, date_trunc('hour',"date") order by "date" desc) as pid_hr_seq
Then write your query against that CTE with where pid_hr_seq = 1.
I have two data sets and I need to merge them in a special way below.
My First data set
data1=data.frame(store=c(12,13),product=c(1,2))
data1
store product
12 1
13 2
data2=data.frame(product=c(1,1,2,2,2),promo=c("promo1","promo2","promo1","promo2","promo3"))
data2
product promo
1 promo1
1 promo2
2 promo1
2 promo2
2 promo3
Desired data set below;
store product numberofpromo promo
12 1 2 promo1;promo2
13 2 3 promo1;promo2;promo3
Thank you
data=data.frame(data2%>%
group_by(product) %>%
summarise(promotion=paste(promo,collapse=";"),
promo_say=n()))
After finding this, you need to the join
I have the following records in my data base
fields: brand |model |attributes
----------------------------
record 1) apple |iPhone|6s space grey sprint
record 2) audi | a6 |quattro coupe
How can I store the attributes into different column names, with version/color/carrier columns for the first record and engine/type as the different columns for second record. The table should have 5 total columns for 1st record and 4 columns for the second record.
How do I achieve this? Should I split the table? If there are million products and each have varied length attributes then the number of columns in the table will be long. Whats the efficient way of doing this?
You can store the related attributes in a second table.
record|attribute|value
------+---------+-----
1 |version |6s
1 |color |space grey
1 |carrier |sprint
2 |engine |quattro
2 |type |coupe
This would allow for an item to have any number of (optional) attributes.
I want to obtain reccomendations on the most purchased item for an order with a specific item so for example if I have such table
user order items purchased
1 1 1,3
1 2 2,3
2 1 3
3 1 2,4
3 2 1,2,4
if I visit the page of item 2 I want item 4 as suggested product because it is present on the rows 2,4 and 5 while the item 3 is present only on row 2 (I am considering just orders with the item 2 in it) (note that the item 3 is the most purchased but I don't want it as suggested since I am looking at item 2). What kind of problem is this? Is it an item reccomender? Is it doable in Mahout or should I implement it by hand? Since it is not possible to model multiple preferences per same user and item, I have thought to convert the string user_order to userId.
Thanks very much
Yes this is a very simple recommender problem. I think you want to ignore 'order'. So your data is more like:
1,1
1,3
1,2
2,3
3,2
3,4
3,1