Last active
December 15, 2015 03:29
-
-
Save gmile/5194954 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
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 |
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' | |
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 |
Author
gmile
commented
Aug 17, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment