Adam Adam - 1 year ago 83
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| user})
@user = @holder.each {|i| if i.alive @user << i}


Answer Source

Use a scope to find all alive users.

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

Then you can do this:

@alive_users = User.alive
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download