Solutons Lounge

ruby on rails – How to use $in with Mongoid?


How I setup my data in an rspec

let(:first_origin) { create(:origin, owner: "123@tenant") }
let(:second_origin) { create(:origin, owner: "123@tenant") }

create_list(:ticket, 10, origin: first_origin)
create_list(:ticket, 10, origin: second_origin)
create_list(:ticket, 5)

The model ticket belongs_to origin.
What I want to accomplish is query all tickets with owner “123@tenant”, using origin_id and $in.
My query is failry simple:

query.in(origin_id: origins)

where origins is an array of the two IDs (tried both the $oid and to_s to have an array of strings.
It always returns all Tickets, count is 25, should be 20. If I try to loop over the results and print origin_id I can clearly see that all origin_id are included and the total count is 25. I’ve tried where(:origin_id.in => origins) as well, same thing.

Latest mongoid and mongodb, latest ruby.

I’m sure it’s my fault, but can’t figure it out. Anybody has any idea?



Source link

Exit mobile version