Skip to content

Instantly share code, notes, and snippets.

@kitop
Last active October 17, 2020 23:18
Show Gist options
  • Save kitop/8af6098b40eea578c3203fe48c4a9813 to your computer and use it in GitHub Desktop.
Save kitop/8af6098b40eea578c3203fe48c4a9813 to your computer and use it in GitHub Desktop.
Mongo relationship issue
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mongoid', '~> 7.1.4'
gem 'minitest', '~> 5.14.1', require: ['minitest', 'minitest/autorun']
end
class Parent
include Mongoid::Document
belongs_to :child
end
class Child
include Mongoid::Document
has_one :parent
def a_public_method
'public'
end
private
def a_private_method
'private'
end
end
class TestRelationships < Minitest::Test
def test_child_private_methods_stay_private
parent = Parent.new
child = Child.new
parent.child = child
assert_raises NoMethodError do
child.a_private_method
end
assert_raises NoMethodError do
parent.child.a_private_method
end
end
def test_child_methods_are_defined
parent = Parent.new
child = Child.new
parent.child = child
assert_equal 'method', defined?(child.a_public_method)
assert_equal 'method', defined?(parent.child.a_public_method)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment