I have created an application where I am allowing users to log their Workouts.
The user has the ability to keep a private or public log of their workouts and is denoted by a check_box field that passes the integer 1 to the workout.share column. The private log is viewable through the workouts_controller where I am limiting all output by filtering for current_user.
workouts_controller.rb
@workouts = current_user.Workouts.all
The public workouts are shown through a separate community_controller and there I call the workouts like this
community_controller
@workouts = Workouts.all
and then filtering the results in the view with the following
<% @workouts.each do |workout| %>
<% if workout.share == 1 %>
...
<% end %>
<% end %>
Best I can tell this is not the preferred way to do this and my suspicion is that I want a named_scope such that I can create a new variable `@shared_workouts’. That said I am not familiar with named scopes so could use some help on where to put what and the correct syntax.
If you are using rails 2, use the following:
If you are using rails 3, use this instead:
Then in the community controller, you could simply use
@workouts = Workouts.shared.all