Skip to content

Instantly share code, notes, and snippets.

@gmile
Last active December 15, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmile/5194954 to your computer and use it in GitHub Desktop.
Save gmile/5194954 to your computer and use it in GitHub Desktop.
require 'pry'
require 'ffaker'
require 'active_record'
database = 'test'
config = {
adapter: 'mysql2',
encoding: 'utf8',
username: 'root',
host: '127.0.0.1',
port: 3306
}
ActiveRecord::Base.configurations = { 'test' => config }
ActiveRecord::Base.establish_connection(:test)
class Architect
def add_brands
brands = 3.times.map { |_| { name: Faker::Product.brand } }
Brand.create(brands)
puts "Added 3 brands..."
end
end
@from_scratch = false
ActiveRecord::Base.connection.recreate_database(database, config) if @from_scratch
binding.pry
ActiveRecord::Base.configurations = { 'test' => config.merge(database: database) }
ActiveRecord::Base.establish_connection(:test)
require './db/schema.rb' if @from_scratch
(ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table|
Object.const_set(table.classify, Class.new(ActiveRecord::Base))
end
Architect.new.add_brands
gem 'rails'
gem 'foreigner'
require 'active_record'
require 'foreigner'
begin
config = {
'database' => 'temp_database',
'adapter' => 'postgresql',
'username' => 'vagrant',
'password' => 'vagrant'
}
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.create_database(config['database'], config)
ActiveRecord::Base.establish_connection(config)
Foreigner.load
ActiveRecord::Schema.define do
create_table :dealers, :force => true do |t|
t.integer :autosocial_group_id, null: true # null: false
t.boolean :no_group, default: true
end
create_table :autosocial_groups, :force => true do |t|
end
add_foreign_key "dealers", "autosocial_groups", name: 'my_fk'
end
class Dealer < ActiveRecord::Base
belongs_to :autosocial_group
end
class AutosocialGroup < ActiveRecord::Base
has_many :dealers, dependent: :nullify
end
group = AutosocialGroup.create
dealer = Dealer.create(autosocial_group_id: group.id)
puts Dealer.first.attributes
group.destroy
puts Dealer.first.attributes
ensure
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database config['database']
end
@gmile
Copy link
Author

gmile commented Aug 17, 2013

ActiveRecord::Base.connection.execute("SELECT * FROM information_schema.columns WHERE table_schema = 'public'").map { |x| x['column_name'] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment