Created
June 26, 2013 07:39
-
-
Save Empact/5865483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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