ruby - How do you call "contains?" on a MongoMapper Array in Rails 3? -


i want know how check if array element exists inside of mongomapper array. this question closest find, addresses queries rather using document have.

my user model contains line

key :roles, array 

the 'roles' array contains strings such 'admin' or 'user.' authorization, need call following method on instance of user:

if user.roles.contains?('admin')     # administrative stuff. end 

but when try call 'contains?' ruby complains there no such method:

nomethoderror (undefined method `contains?' #<array:0x007fc845cd8948>):     app/models/ability.rb:11:in `initialize'     app/controllers/settings_controller.rb:5:in `index' 

if there's no way this, how convert array ruby array call 'contains?'? calling to_a isn't doing it:

if user.roles.to_a.contains?('admin')  # etc... 

i'm using rails 3.2.13, ruby-1.9.3-p392, , mongomapper 0.12.0 on mountain lion.

the function looking include?, expression be: user.roles.include?('admin')

however since mentioned mongomapper, if preforming query on roles array fallowing:

user.where( :roles => 'admin' ) 

you can search array array

user.where( :roles.in => ['admin'] ) 

for query admin or user can do:

user.where( :$or => [{:roles => 'admin'},{:roles => 'user'}] ) 

and can , same:

user.where( :$and => [{:roles => 'admin'},{:roles => 'user'}] ) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -