Is there any way I can use the :limit and :order options in the find method. I'm trying to sort activities by descending order, such that the newest activities are shown. However, when I try to use the (:all, :limit => 5, :order=> 'Date desc) I get an error. I need to limit to only 5 records, and when I disregard the order option, it works but not what I need...
Thanks
I think you missed a quote in your example.
Model.find(:all, :limit => 5, :order=> 'created_at desc')
Make sure that a date column exists in your table.
Is your date column actually called date? First, I would change that, date is the name of a function in most databases, that could be the cause of the error you are seeing. Rails uses created_at, updated_at, etc, so following that naming scheme will make your code more readable to future maintainers.
You could try to quote the column name in back-ticks:
:order => "`date` desc"
Related
I'm trying to order my default_scope in a way that the itens with the boolean important = true show first, and all order by created_at desc.
So, I have the following code:
default_scope order_by(:important => :desc, :created_at => :desc)
But, looks like important field order is ignored.
How can I made it work?
Thanks in advance
EDIT:
I just shift the order of the order params and it works:
default_scope order_by(:created_at => :desc, :important => :desc)
Just that simple.
This is an example on how to sort two columns in rails (probably you might have to slightly modify it to match your requirement), But I think you get the idea
<Model>.all(:order => 'important, created_at')
HTH
What I would like to find is all Events, where event.event_date is in the future, and only get the top 3 events sorted by how many Users the event has associated with it. Events and Users are joined by a HABTM relationship. Here's what I tried:
#popular_events = Event.where("event_date >= ?", Time.now)
.find(:all,
:joins => :users,
:group => 'event_id',
:order => "users.count DESC",
:limit => 10 )
I've tried a few other things with no luck. It is saying users.count is not a valid column.
believe it or not, this is a pain in the b*tt to do this with ActiveRecord. You will more or less have to rely on raw SQL to do it. Some answers here : http://www.tatvartha.com/2009/03/activerecord-group-by-count-and-joins/
I have a searches table, which has all the searches that get run on our site. I want to pull up the most popular searches. Like say there are 130 records with the column of phrase being "cheese", how do I sort the results by count and return them in order of most to least using active record?
Taken from the example I linked in the comments above.
Searches.find(:all, :select => '*, count(*) AS count, phrase', :group => 'phrase', :order => 'count DESC')
Although I just tried this on my own sqlite db and it worked fine (rails 3)
Searches.count(:all, :group => 'phrase', :order => 'count(*) DESC')
I have the parent model of user and user has_many :events. From the user view, how can I find the most recent datetime (event.time) of event?
I'm thinking that a find_by will work, but I'm not sure on how to do that.
Something like this.
user.events.find(:first, :order => "time DESC")
You can read more here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001777
user.events.find(:all, :order => 'time desc', :limit => 100)
where limit is number of recent events you need or:
user.events.find(:first, :order => 'time desc')
if you need one most recent event.
user.events.find(:first, :order => 'time desc').time
like this you can get the event time
Just in case anyone stumbles upon this, the find first helper:
user.events.find(:first, :order => 'time desc').time
is now deprecated and removed as of Rails 3.2. You should use
user.events.order("time desc").first.try(:time)
instead.
i got this error:
SQLite3::SQLException: no such column: apis.name: SELECT * FROM examples WHERE ("apis"."name" = 'deep')
my code
Api.find :all, :from => params[:table_name], :conditions => {:name => 'deep' }
I need to make a back end rails application which will be used by a silverlight application. one of the requirements is to fetch simple data from the database. i need to be able to query different tables with the same code.(my app has 2000 tables!)
i think it does not make sense for rails to put in "apis" in the WHERE clause. is there any speciic reason for this?
It does that so when joins are performed, the where clauses will line up with the right tables' columns. This is handy most of the time, but in your particular case causes issues.
What you could do is use the other conditions syntax, which will not add rails table names to the attributes, but still sanitize the inputs properly.
Api.find :all, :from => params[:table_name], :conditions => ['name = ?','deep']