【问题标题】:Sorting by joined table value with Rails使用 Rails 按连接表值排序
【发布时间】:2014-03-14 00:06:31
【问题描述】:

我正在尝试根据连接表上的字段值对项目列表进行排序。到目前为止,这是我所得到的:

FeaturedEvent.joins(:event).order('event.start_datetime').limit(5)

这在我看来是正确的,但是当它运行时它返回一个关于缺少 FROM 语句的 Postgres 错误:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "event" LINE 1: ...ts"."id" = "featured_events"."event_id" ORDER BY event.star... ^ : SELECT "featured_events".* FROM "featured_events" INNER JOIN "events" ON "events"."id" = "featured_events"."event_id" ORDER BY event.start_datetime LIMIT 5

我尝试了this post 中关于将排序置于默认范围内的建议,但出现了同样的错误——我猜这是 Postgres 的问题。

我该如何解决这个问题?

谢谢!

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-4


    【解决方案1】:

    试试

    FeaturedEvent.includes(:event).order('events.start_datetime').limit(5)
    

    在您的订单中,表名应该是真正的数据库表名。

    我猜,表名一定是events

    【讨论】:

    • @krishna sahoo : include 不应该是 includes 吗?你能看看这个link,当我在(postgres db)方法中使用references时,我得到未定义的方法references ActiveRecord::Relation查询'asdsdfsdf'
    • @krishna sahoo : include 不应该是 includes 吗?你能看看这个link,当我在(postgres db)方法中使用references时,我得到未定义的方法references ActiveRecord::Relation查询@messages = Message.includes(:sender).where("UPPER(body) like UPPER(?) or UPPER(body) like UPPER(?) or UPPER(sender.first_name) like UPPER(?)", "%#{"sus"}%", "%#{"ang"}%", "%#{"bar"}%").references(:people)
    • @zeal,是的,应该是includes
    【解决方案2】:

    你的表名是 events 所以你只需要在 event 中添加 s

    FeaturedEvent.joins(:event).order('events.start_datetime').limit(5)
    

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 2021-11-23
      • 2019-05-02
      相关资源
      最近更新 更多