Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created January 25, 2013 10:38
Show Gist options
  • Select an option

  • Save jonleighton/4633420 to your computer and use it in GitHub Desktop.

Select an option

Save jonleighton/4633420 to your computer and use it in GitHub Desktop.
require "active_record"
require "logger"
puts ActiveRecord::VERSION::STRING
# ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
create_table :people
connection.create_table :accesses do |t|
t.integer :person_id
t.integer :project_id
end
connection.create_table :projects do |t|
t.boolean :trashed
end
end
class Person < ActiveRecord::Base
has_many :accesses
has_many :projects, -> { where trashed: false }, through: :accesses
# has_many :projects, conditions: { trashed: false }, through: :accesses # <-- uncomment for 3.2
end
class Access < ActiveRecord::Base
belongs_to :person
belongs_to :project, -> { where trashed: [ true, false ] }
# belongs_to :project, conditions: { trashed: [ true, false ] } # <-- uncomment for 3.2
end
class Project < ActiveRecord::Base
end
person = Person.create
puts person.projects.to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment