Active record request, where is the value in the array field

I have a table called "Stat" in my MongoDB database in Rails 3.

This table has an array field called "services".

I want to find all Stats that have an array of services that contains the value "lights".

I want to do something like this:

@stats = Stat.all
@stats1 =  @stats.where("services contains lights")
Rails.logger.info "result:  #{@stats1.count}  " 

I tried different things and looked for it many times, found several potential customers, but nothing works. I have four records that should match this query, but the above returns a null set.

I want to do this in rails 3 / mongo?

+1
source share
2 answers

Ok, I found the answer to this question:

@stats = @ stats.where (: services.in => ['lights'])

, :

@stats = @stats.where(: services.nin = > ['lights'])

"nin" "in"

+1

,

@stats = Stat.all

@stats1 =  @stats.where("'lights' = ANY (services)")
0

All Articles