rtconner/laravel-likeable sort after number of likes - laravel-5.1

Using this package https://github.com/rtconner/laravel-likeable I want to do a sort of the articles after the number of likes. Any help is appreciated. Thank you!

$articles = Article::with('likeCounter')
->leftJoin('likeable_like_counters', 'aforisms.id', '=', 'likeable_like_counters.likable_id')
->select('articles.*', 'likeable_like_counters.likable_id', 'likeable_like_counters.count')
->orderBy('likeable_like_counters.count', 'desc')
->paginate(40);

Related

Rails find all order, found 0 results expected 2

I'm having trouble sorting my table based on the votes column, votes is a multivalued attribute so I use count and try to sort it based on that. here is the code
#topics = Topic.find(:all,:order=>'#topic.votes.count DESC')
Rails returns an error that says
ActiveRecord::RecordNotFound in TopicsController#index
Couldn't find all Topics with 'id': (all, {:order=>"#topic.votes.count DESC"}) (found 0 results, but was looking for 2)
Just starting out with rails and still confused with some things, your help will be much appreciated.
this is Rails 2 syntax being used in higher Rails version.
You can do something like this:
Topic.joins(:votes).group('topics.id').order('count(topics.id) DESC')
Instead of join include is best and faster option
Topic.include(:votes).group('topics.id').order('count(topics.id) DESC')
Thanks to ahmed and snehal, I was able to find out the answer I'm looking for, which is
Topic.left_outer_joins(:votes).group('topics.id').order('count(topics.id) DESC')

distinct for sorting with ransack

i was trying to sort values of a table with sort_link from Ransack. i found an example which is really helpful. but, i think i'm having problem with 'distinct'. i got this error 'ORDER-BY expressions must appear in SELECT list; DISTINCT'.
my code looks like this :
q =user.joins(:buyer).order('users.name')
ransack = params["filter"]
#search = q.search(ransack)
#users = #search.result(:distinct=>true)
do i forget something? thanks in advance!
As per this issue you might be able to solve this with an ActiveRecord joins query or select query to add the columns needed, for example:
q = user.order('users.name')
ransack = params["filter"]
#search = q.search(ransack).
result(:distinct=>true).
joins(:buyer).
includes(:buyer)
#users = #search.all
You have distinct as true. Ransack called the distinct value in the wrong manner with Postgresql, thefore you getting this error. If you use another database, you mostlikely would not.
So, if you make distinct false, you will not get this error. If you want to know why you need distinct, check out:
http://www.w3schools.com/sql/sql_distinct.asp
I am trying to find a way to include distinct if I need it. when I find out, I will edit this answer.

activerecord: check first X chars of column

I'd like to check the first two chars of a number straight in my model. First I define the number of the current logged in user (devise):
user_number = current_user.number.first(2)
then I want to take that value and check it within a where statement in a "number" mobel, so I tried this
#numbers = Number.where(:number_value.first(2) => user_number)
which is obviously the same as
#numbers = Number.where(:number_value.first(2) => current_user.number.first(2))
No, that does not work.
How can I check the first 2 chars of the :number_value column in my model?
Any help is appreciated.
Many thanks.
Solution (SQLite)
#numbers = Number.where("number_value like '" + current_user.number.first(2) + "%'")
since this is not lazy loading I'm not convinced yet that it is the smartest solution. if you know any better, would be cool if you can share
First, you should read the ActiveRecord query guide. I'd also imagine that there's a much more straight forward way for you to accomplish your goal.
But, to answer your specific question, here's an approach that'd work with Postgresql.
Number.where("number_value::text like ?", current_user.number.to_s[0,2] + "%")

Find all posts with more than a defined number of comments

I have a simple rails 3 blog app where posts have many comments and comments belong to a post.
I want to create a scope that will fetch all posts that have more than 5 comments.
What's the best way of doing this without a counter cache column.
Like this, perhaps?
Post.select('posts.*, count(comments.id) as comment_count').
joins(:comments).
group('posts.id').
having('comment_count > 5')
In Postgres 9.1 I had to set things up like this as postgres isn't happy putting conditions on calculated fields or something like that.
Post.select('posts.*, count(comments.id) as comment_count').
joins(:comments).
group('posts.id').
having('count(comments.id) > 5')
Great answer from noodl... thanks for that!
I needed to find - to stick with the example classes of the original question - the 4 posts that were most recently commented on... a slight variant of noodl's answer does the trick:
Post.select('posts.*, max(comments.created_at) as last_commented_at').
joins(:comments).
group('posts.id').
order('last_commented_at DESC').
limit(4)
Thanks!

Getting value from query

I'm having problems with getting a value from a single column call.
select sum(price) as testPrice from sales where quantity > 10
but when I inspect the resultset, I get the following,
[#"5"}>]
Any help is appreciated, thank you.
That is a very strange result. I am curious why you are not writing the query using ActiveRecord methods:
#testPrice = Sale.sum(:price, :conditions => "quantity > 10")

Resources