Q:

where for querying an array attribute in rails model

I have a user model with an array of roles.
u=User.first
u.roles = ['admin']
v=User.last
u.roles = ['admin', 'member']

How do i query to fetch all users with admin role?
Edit:
roles is a serialized array for the user model
User Model:
serialize :roles

Migration:
add_column :users, :roles, :string, default: []

A:

Depends on what "roles" means:
If it is an association as in
class User < ActiveRecord::Base
  has_many :roles
end

Then you look up the role and fetch all users:
Role.find_by_name("admin").users

or roles is just a column and it is serialised, in which case you can instantiate all users (slow)
User.all.select { |u| u.roles.include? "admin" }

Or query the database directly which is more complex and depends on the adapter.
My opinion: I would avoid using serialised columns, when relations can do. They are cumbersome in every way: Forms, Searches, Selects...

