Rails sql搜索has_one关系

时间:2015-05-20 11:49:24

标签: ruby-on-rails

notices_controller.rb中的此查询:

Notice.includes(:active_comment_relationship).where(active_comment_relationship: {id: nil} ).limit(50)

产生了这个错误:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "active_comment_relationship"

我看不出代码有什么问题。我看过像this这样的答案,据我所知它应该有效。

notice.rb:

has_one :active_comment_relationship, class_name: "Commentrelationship",
                                      foreign_key: "commenter_id",
                                      dependent: :destroy
has_one :supernotice, through: :active_comment_relationship, source: :commentee

2 个答案:

答案 0 :(得分:1)

在includes部分中,您需要关联名称,但在where子句中需要表名,通常是复数名称。在这种情况下,我假设它是这样的:

Notice.includes(:active_comment_relationship).where(commentrelationships: {id: nil} ).limit(50)

答案 1 :(得分:0)

表名必须是复数,:active_comment_relationships

    Notice.includes(:active_comment_relationships).where(active_comment_relationships: {id: nil} ).limit(50)