Trying to add a scope to my transactions model to return the last 10 transactions by created_at
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.
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.
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.
scope :most_recent, order(created_at: :desc).limit(10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With