ruby on rails - How to count number of instances in a db that have both characterics -


an organization can have multiple members, , member belongs 1 organization. member can admin or not.

an admin member should able deleted if organization has 2 admin members or more. achieve through if statement.

how can ruby count number of members organization have admin characteristic? have:

<% if (member.admin && @organization.members.admin.count < 2) %> 

this produces error message:

undefined method 'admin' 

it second part after && doesn't work. if remove part produces no errors.

given @organization.members.count calculates number of members organization, thought @organization.members.admin.count might calculate number of members of organization admin=true. apparantly incorrect. how should count number of members?

just add admin scope member model:

class member < activerecord::base   scope :admin, -> { where(admin: true) } end