【问题标题】:Rails: get records through many other recordsRails:通过许多其他记录获取记录
【发布时间】: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)

【问题讨论】:

    标签: ruby-on-rails rails-activerecord


    【解决方案1】:

    has many associations 应该是复数的:

      has_many :favorite_industries
    

    那么您要查找的查询将是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多