Created
May 22, 2015 23:50
-
-
Save tenderlove/8e834be1baa3290ff36a 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 '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