Skip to content

Instantly share code, notes, and snippets.

@cristibalan
Last active August 29, 2015 13:56
Show Gist options
  • Save cristibalan/9137307 to your computer and use it in GitHub Desktop.
Save cristibalan/9137307 to your computer and use it in GitHub Desktop.
# gem 'activerecord', '=4.0.2'
gem 'activerecord', '=4.1.0.rc1'
require 'active_record'
require 'minitest/autorun'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(adapter: "postgresql", host: "localhost", user: "cristi", database: "test")
class Message < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
end
connection.create_table :message_threads_messages, force: true, id: false do |t|
t.references :message
t.references :message_thread
end
has_and_belongs_to_many :message_threads, before_add: :set_as_latest_message
def set_as_latest_message(message_thread)
message_thread.latest_message = self
end
end
class MessageThread < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
t.references :latest_message, null: false
end
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!
m.message_threads << MessageThread.new
assert MessageThread.count == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment