Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created Feb 27, 2014
Embed
What would you like to do?
require 'active_record'
require 'minitest/autorun'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(adapter: "postgresql", host: "localhost", database: "test")
class User < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
end
has_many :profile_views
end
class ProfileView < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
t.integer :user_id
t.timestamps
end
belongs_to :user
end
class BugTest < MiniTest::Unit::TestCase
def test_home
u = User.create!
ProfileView.create!(user: u)
id = User.includes(:profile_views).joins('LEFT JOIN profile_views ON profile_views.user_id = users.id').order('profile_views.created_at NULLS FIRST').first.id
assert u.id, id
end
end
@tenderlove

This comment has been minimized.

Copy link
Owner Author

@tenderlove tenderlove commented Feb 27, 2014

: SELECT  DISTINCT "users"."id", profile_views.created_at NULLS FIRST AS alias_0 FROM "users" LEFT OUTER JOIN "profile_views" ON "profile_views"."user_id" = "users"."id" LEFT JOIN profile_views ON profile_views.user_id = users.id  ORDER BY profile_views.created_at NULLS FIRST LIMIT 1
E

Finished in 0.035102s, 28.4884 runs/s, 0.0000 assertions/s.

  1) Error:
BugTest#test_home:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near "FIRST"
LINE 1: ...INCT "users"."id", profile_views.created_at NULLS FIRST AS a...
                                                             ^
: SELECT  DISTINCT "users"."id", profile_views.created_at NULLS FIRST AS alias_0 FROM "users" LEFT OUTER JOIN "profile_views" ON "profile_views"."user_id" = "users"."id" LEFT JOIN profile_views ON profile_views.user_id = users.id  ORDER BY profile_views.created_at NULLS FIRST LIMIT 1
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:833:in `async_exec'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:833:in `block in exec_no_cache'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
    /Users/aaron/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:833:in `exec_no_cache'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:965:in `select'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:31:in `select_all'
    /Users/aaron/git/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:69:in `select_all'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:382:in `limited_ids_for'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:369:in `apply_join_dependency'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:330:in `find_with_associations'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation.rb:603:in `exec_queries'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation.rb:487:in `load'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation.rb:231:in `to_a'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:467:in `find_nth_with_limit'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:459:in `find_nth'
    /Users/aaron/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:132:in `first'
    ../9117192/gist14109.rb:26:in `test_home'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
[aaron@higgins rails (master)]$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment