Skip to content

Instantly share code, notes, and snippets.

@rsim
Created February 7, 2011 16:28
Show Gist options
  • Save rsim/ca8d82d1dd70f73c51de to your computer and use it in GitHub Desktop.
Save rsim/ca8d82d1dd70f73c51de to your computer and use it in GitHub Desktop.
require "rubygems"
gem "activerecord", "3.0.3"
# $:.unshift "../../rails/activerecord/lib"
gem "arel", "2.0.7"
# $:.unshift "../../arel/lib"
require "active_record"
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => "test.sqlite3"
ActiveRecord::Base.connection.instance_eval do
create_table "users", :force => true do |t|
t.string "name"
end
create_table "user_responsibilities", :force => true do |t|
t.integer "user_id"
t.integer "responsibility_id"
t.boolean "active"
end
create_table "responsibilities", :force => true do |t|
t.string "name"
t.boolean "active"
end
end
class User < ActiveRecord::Base
has_many :user_responsibilities, :conditions => {:active => true}
has_many :responsibilities, :through => :user_responsibilities, :conditions => {:active => true}
end
class UserResponsibility < ActiveRecord::Base
belongs_to :user
belongs_to :responsibility
end
class Responsibility < ActiveRecord::Base
has_many :user_responsibilities
end
puts User.joins(:user_responsibilities).to_sql
# => SELECT "users".* FROM "users"
# INNER JOIN "user_responsibilities" ON "user_responsibilities"."user_id" = "users"."id" AND "user_responsibilities"."active" = 't'
puts User.joins(:responsibilities).to_sql
# with Rails 3.0.3 (and also 3-0-stable) and Arel 2.0.7
# => SELECT "users".* FROM "users"
# INNER JOIN "user_responsibilities" ON "users"."id" = "user_responsibilities"."user_id"
# INNER JOIN "responsibilities" ON 0
# with Rails 3.0.3 (and also 3-0-stable) and Arel master
# => SELECT "users".* FROM "users"
# INNER JOIN "user_responsibilities" ON "users"."id" = "user_responsibilities"."user_id"
# INNER JOIN "responsibilities" ON "responsibilities"."active" = 't'
# with Rails master and Arel master
# => SELECT "users".* FROM "users"
# INNER JOIN "user_responsibilities" "user_responsibilities" ON "users"."id" = "user_responsibilities"."user_id" AND "responsibilities"."active" = 't'
# INNER JOIN "responsibilities" ON "responsibilities"."id" = "user_responsibilities"."responsibility_id" AND "responsibilities"."active" = 't'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment