【问题标题】:Named scope with a belongs_to join in Ruby on Rails 4在 Ruby on Rails 4 中使用 belongs_to 加入的命名范围
【发布时间】:2014-10-12 14:48:56
【问题描述】:

我有两个模型

class Faq < ActiveRecord::Base
  belongs_to :faq_type
end

class FaqType < ActiveRecord::Base
  has_many :faqs
end

我想创建一个仅获取 FaqType 名称为“student”的常见问题的学生范围

我尝试了很多变体

class Faq < ActiveRecord::Base
  belongs_to :faq_type

  scope :student, -> { joins(:faq_type).where('faq_type.name = ?', 'Student') }
end

这些似乎都不起作用。

一般错误消息类似于..

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "faq_type"
SELECT "faqs".* FROM "faqs" INNER JOIN "faq_types" ON "faq_types"."id" = "faqs"."faq_type_id" WHERE (faq_type.name = 'Student')

谁能解释这里发生了什么,阅读文档http://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope 似乎表明这是正确的方法。

谢谢!

【问题讨论】:

    标签: ruby-on-rails activerecord join ruby-on-rails-4


    【解决方案1】:

    可能你错过了这部分

    .where('faq_type.name = ?', 'Student') }
    

    Student.name 或其他内容。请仔细检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2017-03-28
      • 2014-07-03
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多