Skip to content

Instantly share code, notes, and snippets.

@Empact
Created June 26, 2013 07:51
Show Gist options
  • Save Empact/5865555 to your computer and use it in GitHub Desktop.
Save Empact/5865555 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 :firms, force: true do |t|
end
create_table :clients, force: true do |t|
t.integer :firm_id
t.string :description
end
end
class Firm < ActiveRecord::Base
has_many :clients
end
class Client < ActiveRecord::Base
belongs_to :firm
end
firm = Firm.new
firm.clients << Client.create!
firm.save!
puts "#{firm.clients.count} clients. 1 expected."
updated_clients = firm.clients.update_all(description: 'Great!')
puts "#{updated_clients} clients updated. 1 expected."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment