Rails:通过许多其他记录获取记录

Posted

技术标签:

【中文标题】Rails:通过许多其他记录获取记录【英文标题】:Rails: get records through many other records 【发布时间】:2022-01-08 09:46:48 【问题描述】:

我有带有 3 个表的 rails 应用程序:

#jobs
  has_many :favorite_industries
#industries
  has_many :favorite_industries
#favorite_industry
  belongs_to :industry
  belongs_to :job

我怎样才能在多个行业找到工作,例如industry_ids(1, 2, 3, 4, 5, 6)

【问题讨论】:

【参考方案1】:

has many associations 应该是复数的:

  has_many :favorite_industries

那么您要查找的查询将是

Job.joins(:favorite_industries).where(industry_id: [1,2,3,4,5,6])

【讨论】:

以上是关于Rails:通过许多其他记录获取记录的主要内容,如果未能解决你的问题,请参考以下文章