Last active
January 30, 2021 14:13
-
-
Save yosiat/37371424bb12236c0a4bb150bc8533ec 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
# frozen_string_literal: true | |
require 'torque-postgresql' | |
require 'byebug' | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
ActiveRecord::Base.establish_connection( | |
adapter: "postgresql", | |
database: "test", | |
encoding: "unicode", | |
host: "localhost", | |
port: "5432", | |
password: "12345", | |
username: "test") | |
ActiveRecord::Schema.define do | |
drop_table "employees", if_exists: true | |
drop_table "projects", if_exists: true | |
drop_table "departments", if_exists: true | |
create_table "employees" do |t| | |
t.string "name" | |
t.timestamps | |
end | |
create_table "projects" do |t| | |
t.string "title" | |
t.timestamps | |
end | |
# This reproduces the issue | |
execute "alter table projects add column employees_ids integer[] default '{}'::integer[]" | |
create_table "departments" do |t| | |
t.string "name" | |
t.integer "project_id" | |
t.timestamps | |
end | |
end | |
class Employee < ActiveRecord::Base | |
end | |
class Department < ActiveRecord::Base | |
after_initialize :do_something, if: :new_record? | |
belongs_to :project | |
def do_something | |
puts "\n[Department] after_initialize called" | |
puts "[Department] after_initialize #{self.project&.employees&.count}" | |
puts "" | |
end | |
end | |
class Project < ActiveRecord::Base | |
belongs_to_many :employees, foreign_key: "employees_ids" | |
validates_presence_of :departments | |
has_many :departments | |
end | |
#ActiveRecord::Base.logger = Logger.new(STDOUT) | |
class BugTest < Minitest::Test | |
def test_new | |
employee = Employee.create! | |
project = Project.new(employees: [employee]) | |
project.departments << Department.new(project: project) | |
project.save! | |
assert_equal 1, project.employees.count | |
assert_equal 1, project.employees_ids.count | |
end | |
end |
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
# frozen_string_literal: true | |
require 'torque-postgresql' | |
require 'byebug' | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
ActiveRecord::Base.establish_connection( | |
adapter: "postgresql", | |
database: "test", | |
encoding: "unicode", | |
host: "localhost", | |
port: "5432", | |
password: "12345", | |
username: "test") | |
ActiveRecord::Schema.define do | |
drop_table "employees", if_exists: true | |
drop_table "projects", if_exists: true | |
drop_table "departments", if_exists: true | |
create_table "employees" do |t| | |
t.string "name" | |
t.timestamps | |
end | |
create_table "projects" do |t| | |
t.bigint "employees_ids", array: true | |
t.string "title" | |
t.timestamps | |
end | |
create_table "departments" do |t| | |
t.string "name" | |
t.integer "project_id" | |
t.timestamps | |
end | |
end | |
class Employee < ActiveRecord::Base | |
end | |
class Department < ActiveRecord::Base | |
after_initialize :do_something, if: :new_record? | |
belongs_to :project | |
def do_something | |
puts "\n[Department] after_initialize called" | |
puts "[Department] after_initialize #{self.project&.employees&.count}" | |
puts "" | |
end | |
end | |
class Project < ActiveRecord::Base | |
belongs_to_many :employees, foreign_key: "employees_ids" | |
validates_presence_of :departments | |
has_many :departments | |
end | |
#ActiveRecord::Base.logger = Logger.new(STDOUT) | |
class BugTest < Minitest::Test | |
def test_new | |
employee = Employee.create! | |
project = Project.new(employees: [employee]) | |
project.departments << Department.new(project: project) | |
project.save! | |
assert_equal 1, project.employees.count | |
assert_equal 1, project.employees_ids.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment