Rails Mongoid使用多对多where子句进行搜索

时间:2015-05-25 13:07:55

标签: ruby-on-rails mongodb ruby-on-rails-4 mongoid

我有" Vaga":

vaga.rb

class Vaga
  include Mongoid::Document

  has_and_belongs_to_many :areas, autosave: true

结果:

#<Vaga _id: 5524940d706f6c09180c0000, area_ids: [BSON::ObjectId('5524939b706f6c09180b0000'), BSON::ObjectId('5436721b706f6c0918cf0000')], titulo: "Ruby on Rails developer">

&#34; Area&#34; 的 area.rb

class Area
  include Mongoid::Document

  has_and_belongs_to_many :vagas

我怎样才能得到所有&#34; vagas&#34;有一系列&#34;区域&#34;?

我正在尝试这个:

vagas = Vaga.all('areas.id' => '5524939b706f6c09180b0000')

但它不会工作

1 个答案:

答案 0 :(得分:3)

你可以试试这个

vagas = Vaga.where(area_ids: ['5524939b706f6c09180b0000'])

area = Area.where(id: '5524939b706f6c09180b0000').first
vagas = area.vagas

希望这有帮助!