Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created February 25, 2014 19:56
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/9216480 to your computer and use it in GitHub Desktop.
Save tenderlove/9216480 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
class Message < ActiveRecord::Base
connection.create_table(table_name)
connection.create_table :message_threads_messages, id: false do |t|
t.references :message
t.references :message_thread
end
has_and_belongs_to_many :message_threads, before_add: :callme
has_many :topics
has_many :people, through: :topics, before_add: :callme
def callme(model)
p :NEW => [model.class, model.new_record?]
end
end
class Topic < ActiveRecord::Base
connection.create_table(table_name) { |t|
t.references :message
t.references :person
}
belongs_to :person
belongs_to :message
end
class Person < ActiveRecord::Base
connection.create_table(table_name)
has_many :topics
end
class MessageThread < ActiveRecord::Base
connection.create_table(table_name) { |t| t.references :latest_message }
has_and_belongs_to_many :messages
belongs_to :latest_message, class_name: 'Message'
end
class BugTest < MiniTest::Unit::TestCase
def test_before_add
m = Message.create!
puts "HABTM"
m.message_threads << MessageThread.new
m.topics << Topic.new
puts "HM:T"
m.people << Person.new
end
end

4-0-stable

HABTM
{:NEW=>[MessageThread(id: integer, latest_message_id: integer), true]}
HM:T
{:NEW=>[Person(id: integer), false]}
.

master

HABTM
{:NEW=>[MessageThread(id: integer, latest_message_id: integer), false]}
HM:T
{:NEW=>[Person(id: integer), false]}
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment