25 Mar '17, 6am

Order query by number of times one model has another

I have the following structure: User model: id, .. Event model: id, .. UserMapEvent model: user_id, event_id For example the UserMapEvent table might have the following records: User_id - Event_id 1 - 1 1 - 2 3 - 1 4 - 1 I'm using this structure to save when a user likes an event. Now, if I'm in the shows controller and I know the event's id, I can get the number of times the user liked the event like: likes = UsersMapEvent.where("event_id = ?", event.id).count How do I get, for example, the top 3 liked events? Do I have to join the 2 tables? The only solution that I could think of is to use Event.all and for each to do the exact same thing, but it sounds stupid and I assume this is not one of those cases 'if it's stupid and it works is fine'. What type of database operation do I have to use to get my query?

Full article: http://stackoverflow.com/questions/43013308/order-query-b...

Tweets

Elon Musk Tweets Up A Model 3 And Model Y Storm...

gas2.org 25 Mar '17, 4pm

That sure makes it sound like Musk expects the Model 3 to be ready for autonomous driving when production begins later thi...

@sookliantan The frequency is 30-45 mins per bu...

myrapid.com.my 27 Mar '17, 3am

Prasarana Integrated Management & Engineering Services Sdn Bhd (PRIME) Prasarana Integrated Development Sdn Bhd (PRIDE) Pr...

Filipina domestic helper cited as model caregiver in Singapore

Filipina domestic helper cited as model caregiv...

globalnation.inquirer.net 26 Mar '17, 4am

Josefina Amparo Hosena (right) carries Leona Heng, who suffers from Edwards syndrome, onto an exercise ball as part of her...

Filipina domestic helper cited as model caregiv...

globalnation.inquirer.net 26 Mar '17, 4am

Josefina Amparo Hosena (right) carries Leona Heng, who suffers from Edwards syndrome, onto an exercise ball as part of her...

#Tesla’s Model 3 is probably the most anticipated #AutonomousCar and #ElectricCar of 2017 https://t.co/kbUiVu7QAB

#Tesla’s Model 3 is probably the most anticipat...

hybridcars.com 25 Mar '17, 12pm

More than the most anticipated electric car of 2017, Tesla’s Model 3 may be the most newsworthy car of any type, but the o...

A sparkle of elegance. Discover the Overseas sm...

vacheron-constantin.com 25 Mar '17, 5pm

Guidelines: for daily use, activities on the surface of the water (e.g. leisure swimming, diving from the pool edge but no...

#How to get attribute of object in Model - Ruby...

abizy.com 04 Apr '17, 10pm

How to Slow Brain Aging by Two Years How to Slow Brain Aging by Two Years Category:Videos Source: YouTube More:Similar Vid...

What IS 'one-pedal driving' in an electric car?

greencarreports.com 24 Mar '17, 1pm

John Voelcker Mar 24, 2017 Follow John Add to circle 52 prototype x pedals Enlarge Photo One-pedal driving is rather like ...

Celebration, Florida may be destroyed in order to save it

Celebration, Florida may be destroyed in order ...

treehugger.com 20 Mar '17, 2pm

Celebration, Florida is an example of what is called New Urbanism, modeled after older towns with compact downtowns, walka...

[Mothership] 9 year old boy poisoned female classmate with Dettol to win the heart of another girl

[Mothership] 9 year old boy poisoned female cla...

mothership.sg 24 Mar '17, 12pm

A 9 year old primary school boy poured Dettol into a female classmate’s water bottle, to win the heart of another. Accordi...

First 'release candidate' Tesla Model 3 driven:...

greencarreports.com 24 Mar '17, 3pm

John Voelcker Mar 24, 2017 Follow John Add to circle First release candidate Tesla Model 3 electric car, from video tweete...

One bag travel: my new rolling backpack, handba...

frugalfirstclasstravel.com 25 Mar '17, 3am

I wasn’t unhappy with the toilet bag I had been using for a number years. It was a great light travel one, but with luggag...

What IS 'one-pedal driving' in an electric car?

greencarreports.com 26 Mar '17, 8am

John Voelcker Mar 24, 2017 Follow John Add to circle 52 prototype x pedals Enlarge Photo One-pedal driving is rather like ...