Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a scope to get the last ten transactions with rails3

Trying to add a scope to my transactions model to return the last 10 transactions by created_at

like image 886
Olivier Avatar asked Jan 20 '11 06:01

Olivier


People also ask

What are scopes how should you use it?

Scopes are custom queries that you define inside your Rails models with the scope method. Every scope takes two arguments: A name, which you use to call this scope in your code. A lambda, which implements the query.

What is Ruby scope?

Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, local, global, instance and class. In addition, Ruby has one constant type. Each variable type is declared by using a special character at the start of the variable name as outlined in the following table.

How do you name a scope in Rails?

Like Ruby class methods, to invoke the named scopes, simply call it on the class object. Named scopes takes two arguments; name and lambda. Use an expressive name. Lambda is a block of code.


1 Answers

scope :most_recent, order(created_at: :desc).limit(10)
like image 189
idlefingers Avatar answered Oct 20 '22 16:10

idlefingers