Alex Rivera | Logout

Rails where() clause on model association?

Asked 2011-06-13T18:24:34.517
56

Suppose I have an Account model which has_many Users. Accounts have a boolean "active" column. How can I get all Users that belong to "active" accounts?

@users_of_active_accounts = User.?

Thanks!

Edit
Report

1 Answer

62

You need to join the accounts table and merge appropriate Account scope:

User.joins(:account).merge(Account.where(:active => true))
answered 2011-06-13T18:40:31.487

Your Answer