Preload only with where in an associated table - aavedula/how-to-notes GitHub Wiki

Back

Rails command

  Member.preload(:population).where(populations: { kind: 2 }.first

Error

Member.preload(:population).where(populations: { kind: 2 } ).first
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "populations"
LINE 1: SELECT  "members".* FROM "members" WHERE "populations"."kind...
                                                 ^
: SELECT  "members".* FROM "members" WHERE "populations"."kind" = $1 ORDER BY "members"."id" ASC LIMIT $2
from /Users/a812863/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:677:in `prepare'
Caused by PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "populations"
LINE 1: SELECT  "members".* FROM "members" WHERE "populations"."kind...
                                                 ^