acreek
acreek

Reputation: 207

Rails filtering partial results

I'm rendering a todo list for a user like so

<%= render @user.todos %>

What I'm trying to figure out is how to show only the ones that have been marked as completed.

Is this something that I should be doing in a controller or model, or is it ok to filter something like that in a view?

Upvotes: 1

Views: 208

Answers (2)

John Beynon
John Beynon

Reputation: 37507

sounds like a named scope on the todo model which returns completed todos so you can then do

<%= render @user.todos.completed %>

Upvotes: 3

Rafal
Rafal

Reputation: 2576

The logic should always go into your Model.

You should create a :scope (rails 3) or :default_scope (Rails 2) in your model and call it in the View directly. Something like completed_todos and then you can call @user.completed_todos. You simply set completed = true in the scope.

Hope this helps.

Upvotes: 1

Related Questions