Adam Adam - 4 months ago 6
Ruby Question

selecting certain users in rails

I'm writing a zombie survival app, and I'm trying to select all my users marked "alive" where :alive is a boolean.
I was writing a private method in my users controller but can't get the ruby right, does anyone have a pointer?

def get_alive
@holder = (User.map {|user| user})
@user = @holder.each {|i| if i.alive @user << i}
end


thanks

Answer

Use a scope to find all alive users.

class User < ActiveRecord::Base
  scope :alive, where(:alive => true)
  # ... the rest of your model ...
end

Then you can do this:

@alive_users = User.alive