Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created January 24, 2011 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcardarella/793838 to your computer and use it in GitHub Desktop.
Save bcardarella/793838 to your computer and use it in GitHub Desktop.
Tests the join sql
require 'rubygems'
require 'test/unit'
require 'arel' # master
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
:database => ':memory:'
)
users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY);}
records_table = %{CREATE TABLE records (id INTEGER PRIMARY KEY);}
records_users_table = %{CREATE TABLE records_users (user_id INTEGER NOT NULL, record_id INTEGER NOT NULL);}
ActiveRecord::Base.connection.execute(users_table)
ActiveRecord::Base.connection.execute(records_table)
ActiveRecord::Base.connection.execute(records_users_table)
class User < ActiveRecord::Base
has_and_belongs_to_many :records
end
class Record < ActiveRecord::Base
has_and_belongs_to_many :users
end
class HBTMTest < Test::Unit::TestCase
def test_sql
expected_sql = %{INNER JOIN "records_users" ON "records_users"."user_id" = "users"."id" INNER JOIN "records" ON "records"."id" = "records_users"."record_id"}
assert_equal expected_sql, User.joins(:records).join_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment