在ActiveRecord查询中使用joins时,出现“RuntimeError: unknown class: Integer (RuntimeError)”错误

编程语言 2026-07-12

我有一个ActiveRecord查询,如下所示:

Appn.where(stage: :final)
        .where('event_time < ?', Time.current)
        .joins('
          meetings ON meetings.application_id = applications.id
          AND meetings.staff_member_id = ?', staff_member.id
        )

但当我尝试访问它产生的结果时(例如调用 .to_a),我看到以下错误:

RuntimeError: unknown class: Integer (RuntimeError)
from /Users/sam/.rbenv/versions/3.4.8/lib/ruby/gems/3.4.0/gems/activerecord-7.2.1.1/lib/active_record/relation/query_methods.rb:1833:in 'block in ActiveRecord::QueryMethods#build_join_buckets'

我已查看过 “RuntimeError unknown class:” 在访问包含join的结果时,但这并不能解释我的情况。

解决方案

由此发现我有两个问题。第一个是我的连接字符串没有 包括连接类型。我必须以(在我的情况下)inner join 开头。

不过,这还不够——我还必须改用 = ? 语法,并改用 插值(interpolation)

这就得到了最终版本:

Appn.where(stage: :final)
        .where('event_time < ?', Time.current)
        .joins("
          inner join meetings ON meetings.application_id = applications.id
          AND meetings.staff_member_id = #{staff_member.id}
        ")
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章