Skip to content

Instantly share code, notes, and snippets.

@sgrif
Last active August 29, 2015 14:03
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 sgrif/fbb71923cb0bc35df57a to your computer and use it in GitHub Desktop.
Save sgrif/fbb71923cb0bc35df57a to your computer and use it in GitHub Desktop.
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../rails/Gemfile', __FILE__)
require_relative '../rails/load_paths'
require 'active_record'
require 'active_support/all'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection(adapter: :sqlite3, database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :business_units do |t|
t.string :reference
t.timestamps
end
create_table :worksites do |t|
t.string :reference
t.timestamps
end
create_table :worksites_business_units do |t|
t.string :reference
t.belongs_to :business_unit, null: false
t.belongs_to :worksite, null: false
t.timestamps
end
end
class BusinessUnit < ActiveRecord::Base
has_many :worksites, :through => :worksites_business_units
has_many :worksites_business_units
end
class Worksite < ActiveRecord::Base
belongs_to :postal_address
has_many :business_units, :through => :worksites_business_units
has_many :worksites_business_units
end
class WorksitesBusinessUnit < ActiveRecord::Base
belongs_to :worksite
belongs_to :business_unit
end
class WorksiteTest < ActiveSupport::TestCase
def test_save_callbacks_on_has_many_through_when_only_one_side_is_new
bu = BusinessUnit.new(reference: '123')
ws = Worksite.new(reference: '456')
ws.business_units << bu
ws.save!
assert_equal 1, ws.business_units.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment