Skip to content
richlewis14 edited this page Nov 4, 2012 · 1 revision

A scope is defined within the relevant model. A scope enables you to query the model and bring back your query within the model.

Example below

scope :top_countries, order("country_of_origin DESC")

This example is selecting the "Country of origin" column within the model and ordering it in DESC order.We call it :top_countries, but this can be called whatever we want.

To call the results in the view we set this in the relevant controller

 @toprankingcountry = Recipe.top_countries.first

and then in the view itself we use

<%= @toprankingcountry.country_of_origin  %>