Q:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: fakultaets.name

I have 4 models with following relation
class Extmodul
  belongs_to :fakultaet
  belongs_to :studierender
  belongs_to :semester

and these three models have has_many relation with Extmodul
 has_many :extmoduls

Now when I run this 
 Extmodul.includes(:studierender, :semester, :fakultaet).where(semester_id: 4).where("fakultaets.name like ?", "%ad%")

I get this error
2.2.3 :046 >   Extmodul.includes(:studierender, :semester, :fakultaet).where(semester_id: 4).where("fakultaets.name like ?", "%ad%")
  Extmodul Load (1.3ms)  SELECT "extmoduls".* FROM "extmoduls" WHERE "extmoduls"."semester_id" = ? AND (fakultaets.name like '%ad%')  [["semester_id", 4]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: fakultaets.name: SELECT "extmoduls".* FROM "extmoduls" WHERE "extmoduls"."semester_id" = ? AND (fakultaets.name like '%ad%')
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `new'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `prepare'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:306:in `block in exec_query'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:293:in `exec_query'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:39:in `find_by_sql'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:639:in `exec_queries'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:515:in `load'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:243:in `to_a'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:630:in `inspect'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:in `start'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in `start'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /home/bcc/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'2.2.3 :047 > 

But when I run same query with one extra argument it works perfectly
 Extmodul.includes(:studierender, :semester, :fakultaet).where(studierenders:{archiv: false}, semester_id: 4).where("fakultaets.name like ?", "%ad%")

Why is the previous query showing error? How to make it work?

A:

If you want to add conditions to your included models you’ll have to
  explicitly reference them.

The below should work
Extmodul.includes(:studierender, :semester, :fakultaet).where(semester_id: 4).where("fakultaets.name like ?", "%ad%").references(:fakultaets)

