Rails find all order, found 0 results expected 2 - ruby-on-rails

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')

Related

How to search data on created at less than in thinking sphinx

Using thinking_sphinx to fetch data from records. It working fine but facing an issue that not find any to get records on date comparison like created_at greater than or less than. I check their official documentation also Thinking Sphinx
Is thinking sphinx provide that way? If yes then can we do that
It is possible, but it's not entirely obvious.
What you'll need to do is add an extra column in the SELECT clause, and then filter by that. Something along the lines of:
Model.search "pancakes",
:select => "*, IF(created_at > #{1.year.ago.to_i}, 1, 0) AS time_match",
:with => {:time_match => 1}
You will need to have created_at as an attribute in your index file for this to work.

ActiveRecord: query records and get specific item first, sort rest

What is want to do is exactly this:
mysql SQL: specific item to be first and then to sort the rest of the items
but for ActiveRecord.
Going by the solution for mySql provided in above question, i tried:
.order('user_id = 9 DESC')
but it didn't worked. Please help

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.

Will Paginate: Limit number of results

Im using the will paginate gem for ruby. I am using will paginate to get a bunch of people and sorting on a field. What I want is only the first 100 of those. Essentially the top people. I cant seeem to do this. How would i go about it? Thanks
As far as my knowledge goes will_paginate doesn't provide an option for this, I just had a root around in its source too to check, but if you don't mind having a subquery in your conditions it can be done...
people = Person.paginate(page: 10).where('people.id IN (SELECT id FROM people ORDER BY a_field LIMIT 100)').per_page(5)
people.total_pages
=> 20
Replace a_field with the field your sorting on, and this should work for you.
(note, the above uses the current version of will_paginate (3.0.2) for its syntax, but the same concept applies to older version as well using the options hash)
will_paginate gem will take total_entries parameter to limit the number of entries to paginate.
#problems = Problem.paginate(page: params[:page], per_page: 10,
total_entries: 30)
This gives you 3 pages of 10 records each.
people = Person.limit(100).paginate(:per_page => 5)

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!

Resources