Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'pathname'
require 'test/unit'
require 'rubygems'
require 'sqlite3'
require 'activerecord'
begin
require 'ruby-debug'
rescue LoadError, RuntimeError
end
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.silence do
# setup
db_path = Pathname(__FILE__).dirname.join('test.sqlite3')
SQLite3::Database.new(db_path)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => db_path.to_s)
addapter = ActiveRecord::Base.connection
# create tables
addapter.create_table(:embed_codes, :force => true) {} # unless addapter.table_exists?(:embed_codes)
addapter.create_table(:orders, :force => true) {}
addapter.create_table(:references, :force => true) do |t|
t.belongs_to :embed_code
t.belongs_to :order
end
# define models
class EmbedCode < ActiveRecord::Base
has_many :references
has_many :orders, :through => :references
end
class Reference < ActiveRecord::Base
belongs_to :embed_code
belongs_to :order
end
class Order < ActiveRecord::Base
end
end #AR:B.silence
# test
class HasManyThroughTest < Test::Unit::TestCase
def test_joined_table_is_quoted
EmbedCode.create(:orders => [Order.create, Order.create])
assert_nothing_raised do
assert_equal 2, EmbedCode.first.orders.count
end
end
end
__END__
OUTPUT:
~/Desktop| ruby sql_error_test.rb
Loaded suite sql_error_test
Started
Order Create (0.7ms) INSERT INTO orders VALUES(NULL)
Order Create (0.5ms) INSERT INTO orders VALUES(NULL)
EmbedCode Create (0.4ms) INSERT INTO embed_codes VALUES(NULL)
Reference Create (0.2ms) INSERT INTO "references" ("order_id", "embed_code_id") VALUES(1, 1)
Reference Create (0.1ms) INSERT INTO "references" ("order_id", "embed_code_id") VALUES(2, 1)
EmbedCode Load (0.3ms) SELECT * FROM "embed_codes" LIMIT 1
SQL (0.3ms) SELECT count(*) AS count_all FROM "orders" INNER JOIN "references" ON "orders".id = "references".order_id WHERE (("references".embed_code_id = 1))
.
Finished in 0.073007 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment