I have 2 models. Gif, GifStatistic
gif has_many :gif_statistics
GifStatistics has column called state. It can be either like or dislike
What i want to achieve is to query gifs, but order them in highest count of likes
Something like(Pseudo code)
Gif.joins(:gif_statistics).order(count(gif.gif_statistics.state))
How do i achieve this?
Try this query.
Gif.joins(:gif_statistics).select("*, count(gif_statistics.state) as state_count").order("state_count desc")
My personal recommendation is that you create two new fields in the gif model so that you can store the count of like and dislike as and when its created so that you don't have to do such a complex query. Placing the counter cache would improve the speed.
Related
I'm having trouble when querying Users.
My nesting resources are:
resources :users do
resources :photos do
resources :pins
end
end
1.) I have a user model, that has_many :photos.
2.) :photos has_many :pins
I want to list my users on which users have more pins in their photos.
So, I tried:
#members_ordered = User.includes(photos: :pins).group("users.id").group("photos.id").group("pins.id").order('COUNT(pins.id) DESC')
Not working though. Any ideas? Thanks guys
I have two observations, but neither directly fix the code in your example.
First, looking at my output from trying something similar, it seems like you either need quite complex SQL (which really isn't Rails' forte) or several simple queries (which, depending on the size of your app, could hit performance) to achieve this.
A little experimenting doesn't seem to show a significant difference (<1ms) in the time that one more complex query takes compared to that which three simple queries require (as in solution one)
Solution one, if performance is not crucial, for example, if this is a small, low-traffic solution, my instinct would be to add that the User model has_many :pins, through: :photos, which lets you call things like User.includes(photos: :pins).all, then user.pins.count, although, as I've mentioned, this causes a bit more database use.
Solution two, if performance is important, my suggestion would be to cache the count of pins against the user model. This could be as simple as an extra database column to store it, and have a background process (using delayed_job or similar) re-calculate the count each time it changes (so, maybe after_create in the Pin model.
The benefit of this is the slow, time-consuming query only gets run when the value changes, and the rest of the time, the value gets lifted from a single-table SELECT, which should take quite a bit less time than either solution one or the more complex query.
Both of these are less-than-perfect, and I think the most elegant and efficient way of working is to use a combination of a built-in function and a beautifully simple query:
The third solution, which brings together both of these options to some extent, is Rails' counter_cache option. As there are two levels to it, I can't see a native way to include all of these in one query, so we will automatically generate a count for each Photo, then add these up to get the User count.
Create a migration to add a pins_count field to the Photo model, so, in terminal, type;
rails g migration AddPinsCountToPhotos pins_count:integer
Update the belongs_to :photo line of the Pin model to;
belongs_to :photo, counter_cache: true
Now, every time a Pin gets created or deleted, the pins_count column of its Photo will be updated.
Now, to get the values for users;
Create a migration to add a pins_count field to the User model, so, in terminal, type;
rails g migration AddPinsCountToUsers pins_count:integer
Now we need to create an method in the Photo model, which we will run each time a pin is saved, so add this to your Photo model;
def update_user_counts
total_photos = self.user.photos.sum(:pins_count)
self.user.update_attribute(:pins_count, total_photos)
end
Finally, we need to tell Rails to call this whenever a pin is created or updated. We do this with a simple method that just calls the action from the Photo model;
after_save :update_photo_counts
def update_photo_counts
photo.update_user_counts
end
Now, whenever a pin is saved, it automatically updates the Photos pins_count, and then our new method totals the pins_counts from all of the Photos for that user, and saves them to the Users pins_count
I'm trying to obtain a list of articles and order them by their popularity over time. For example, older articles should rank lower even if they have a higher number of views.
In order to do this each article has a view count and a posted date. I'm guessing the simplest way would be to divide the article view count by the date posted... something like:
(view_count+comment_count) / date_posted = trend_score
I'm trying to understand if this is possible with the Django ORM, even if it is raw SQL? Would appreciate any help.
I guess the simpliest and most effective way to do is to add a trend_score field to your model and update it when the model is saved (you neeed to save the model anyways if you have a view count/comment count on it). Then you can easily filter by this field. You can fore sure do it somehow with SQL, but if you have to update the values you need to update already within your model, calculate also the score upon saving.
I have a country model and would like to display the country with the most occurrences, country names are held in the column 'mame', however the country db is pre populated and the relationship is a country
has_many recipes
and recipe
belongs_to country
So far I have
Country.group('name').order('count_name DESC').limit(1).count('name')
but this will not work will it as there are 1 of every country in the table? Do i need to do a count on the number of times the country_id is used? if so what would the syntax be for that? would it be
Recipe.group('country_id').order('count_country_id DESC').limit(1).count('country_id')
or using joins and select
Country.joins(:recipes).select('countries.*, count(country_id) as "country_count"').group(:country_id).order(' country_count desc')
Any pointers appreciated
You can do it by using queries. However, RoR has built in support to achieve the same. It is called Counter Cache.
I can explain here but I think it's better if you follow this screencast.
http://railscasts.com/episodes/23-counter-cache-column
This will give you very good idea how to use counter cache and get what you've tried to achieve.
In my application Users register for Events, which belong to a Stream. The registrations are managed in the Registration model, which have a boolean field called 'attended'.
I'm trying to generate a leaderboard and need to know: the total number of registrations for each user, as well as a count for user registrations in each individual event stream.
I'm trying this (in User.rb):
# returns an array of users and their attendence count
def self.attendance_counts
User.all(
:select => "users.*, sum(attended) as attendance_count",
:joins => 'left join `registrations` ON registrations.user_id = users.id',
:group => 'registrations.user_id',
:order => 'attendance_count DESC'
)
end
The generated SQL works for just returning the total attended count for each user when I run it in the database, but all that gets returned is the User record in Rails.
I'm about to give up and hardcode a counter_cache for each stream (they are fairly fixed) into the User table, which gets manually updated whenever the attended attribute changes on a Registration model save.
Still, I'm really curious as to how to perform a query like this. It must come up all the time when calculating statistics and reports on records with relationships.
Your time and consideration is much appreciated. Thanks in advance.
Firstly as a couple of points on style and rails functions to help you with building DB queries.
1) You're better writing this as a scope rather than a method i.e.
scope attendance_counts, select("users.*, sum(attended) as attendance_count").joins(:registrations).group('registrations.user_id').order('attendance_count DESC')
2) It's better not to call all/find/first on the query you've built up until you actually need it (i.e. in the controller or view). That way if you decide to implement action / fragment caching later on the DB query won't get called if the cached action / fragment is served to the user.
3) Rails has a series of functions to help with aggregating db data. for example if you only wanted a user's id and the sum of attended you could use something like the following code:
Registrations.group(:user_id).sum(:attended)
Other functions include count, avg, minimum, maximum
Finally in answer to your question, rails will create an attribute for you to access the value of any custom fields you have in the select part of your query. e.g.
#users = User.attendance_counts
#users[0].attendance_count # The attendance count for the first user returned by the query
Is it possible to limit an ActiveRecord :include to say only pull one record?
Item.find(:all,
:include => [ :external_ratings, :photos => LIMIT 1 ])
I have a list of items, and each item has between 5 and 15 photos. I want to load a photo id into memory, but I don't need all of them. I just want to preview the first one.
Is there a way to do this with ActiveRecord?
I don't believe it is possible to do it directly, but as finding one record is only a single query, why not do it outside the include?
e.g
first_photo = Photo.find(records.first.photo_id)
After your main find
I'm not going to investigate the detailed query, but I'm thinking find_by_sql is in order for this particular case.