-2

After an afternoon spending in web search. I have to ask you.

In my app I have a list of games which have one or more platforms associated. I want to propose to the user some filter based on the platform.

I know that I have to use named scopes with an attribute. But I don't know how to make link which

NotGrm
  • 111
  • 1
  • 8

1 Answers1

2

If you use :has_and_belongs_to_many a lazy way to do this is get all plataforms and game a uniq array of all game:

@games = @plataforms.map(&:games).uniq

If you use :has_many:

# in your game model
scope :by_plataforms, lambda { |plataforms_ids| where(:plataform_id => plataforms_ids) }

A sample call: Game.by_plataforms([1, 2, 3])

EDIT

To create a route you can use a param in your GamesController to filter by plataform:

def index
  @games = params[:plataform] ? Plataform.find(params[:plataform]).games : Game.all
end

And in the views:

<%= link_to games_path(:plataform => @plataform.id) %>
daniloisr
  • 1,367
  • 11
  • 20
  • My problem is not how to make a scope I know it. But how can I make link in my view to filter my game list. Or to be simple how can I set the scope to use in my controller – NotGrm Dec 14 '12 at 18:38