Skip to content

Instantly share code, notes, and snippets.

@Empact
Created June 26, 2013 07:39
Show Gist options
  • Save Empact/5865483 to your computer and use it in GitHub Desktop.
Save Empact/5865483 to your computer and use it in GitHub Desktop.
gem 'rails', '3.2.13'
require 'active_record'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name
end
create_table :logins, force: true do |t|
t.integer :user_id
t.boolean :active
end
end
class Login < ActiveRecord::Base
belongs_to :user
scope :active, -> { where(active: true) }
end
class User < ActiveRecord::Base
has_many :logins do
def announce
puts "#{proxy_association.owner.name} has #{count} logins"
end
end
end
user = User.create!(name: 'Bob')
user.logins.announce
user.logins.active.announce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment