Home>
Thing I want to do
Expected output
Count duplicates for a particular column (country) in rails
Extract only country and sort by group in descending order of duplication
Users table
id | country | name | group |
---|---|---|---|
1 | Japan | Toda | A |
2 | Korea | Lee Dae Ho | A |
3 | America | Trump | A |
Four | Japan | Erika | A |
Five | Japan | Ashida | A |
6 | America | Gatlin | A |
7 | Iceland | Sherry | B |
8 | Puerto Rico | Johnson | B |
9 | Iceland | Sally | B |
["Japan", "America", "Korea"] #Group A
["Iceland", "Puerto Rico"] #Group B
Practice
I searched various things such as sort, uniq, distinct, but I can't handle it easily.
I'm writing code for a long time now
I'm trying to put country in an array and count the countries in the array but it doesn't work
If there is another good way, please teach me
group = ["A", "B"]
a = 0
country = []
2.times do
a = User.where (group: group [a]). pluck (: country)
country<
Postscript
I'm also trying to handle how many duplicates there were
-
Answer # 1
Related articles
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby on rails 5 - some items are not saved in db when posting from rails form
- ruby on rails - things associated with a foreign key cannot be called in view
- ruby on rails 6 - environment construction of ruby on rails
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby on rails - error in controller when transitioning with rails6 destory action
- ruby on rails - access fails after seed after deploying to heroku
- ruby on rails runtime error cannot be resolved
- ruby on rails - missing required keys: i'm having trouble resolving the [: id] error
- ruby - [rails] i can't install the add favorite button
- bootstrap display in ruby on rails is broken
- ruby on rails - how to transition from the top page
- ruby on rails - dealing with wrong number of arguments (given 1, expected 2)
- ruby on rails - seed fails on heroku
- ruby on rails - cannot pass id to destroy action of intermediate table
- ruby on rails - how to manage prefectures in rails web applications
- ruby on rails - i want to post an image by dragging and dropping
- ruby - efficient writing of rails routing and actions
- ruby on rails - launching unicorn doesn't work
Related questions
- ruby - sass :: syntaxerror on rails s
- javascript - i want the scroll bar to always appear at the bottom of the chat screen
- ruby - i want to use two rails renders
- ruby - the top screen is displayed at http: // localhost: 3000/users/sign_in
- ruby - [rails] the class attribute specified by the div tag enclosed in simple_format is not applied
- ruby on rails removal function
- html - i can't set the regular expression well
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby - dynamic form implementation in nested_form
Let's enjoy SQL ...