Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created May 22, 2015 23:50
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 tenderlove/8e834be1baa3290ff36a to your computer and use it in GitHub Desktop.
Save tenderlove/8e834be1baa3290ff36a to your computer and use it in GitHub Desktop.
require 'active_record'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.connection.instance_eval do
create_table "timesheets"
create_table "projects_timesheets" do |t|
t.integer "project_id"
t.integer "timesheet_id"
end
create_table "projects"
end
class Project < ActiveRecord::Base
has_many :projects_timesheets
end
class ProjectsTimesheet < ActiveRecord::Base
belongs_to :project
belongs_to :timesheet
end
class Timesheet < ActiveRecord::Base
has_many :projects_timesheets
has_many :projects, :through => :projects_timesheets
accepts_nested_attributes_for :projects_timesheets
end
class MyTest < Minitest::Test
def test_has_timesheet
timesheet = Timesheet.new.tap do |timesheet|
timesheet.projects += [Project.new]
end
assert_equal timesheet, timesheet.projects_timesheets.first.timesheet
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment