関連付けのメタデータを取得する方法は次のとおりです。
Mongoid::Relations::Reflections#reflect_on_all_associations - http://rdoc.info/github/mongoid/mongoid/Mongoid/Relations/Reflectionsを参照
現在書かれているように、reflect_on_all_associations への引数として複数のマクロを提供したい場合、マクロは args でなければならないことに注意してください。配列を提供したい場合は、次のテストのように、*macro などでスプラッティングする必要があります。
以下は「古い API」のためのものなので、代わりに上記を使用してください。
Mongoid::Relations::ClassMethods::associations - http://rdoc.info/github/mongoid/mongoid/Mongoid/Relations/ClassMethods:associationsを参照
テスト/ユニット/cali_test.rb
require 'test_helper'
class CaliTest < ActiveSupport::TestCase
def setup
Cali.delete_all
end
test "mongoid fields" do
address = Address.new(state: 'NJ', city: 'New Providence')
cali = Cali.create(address: address)
assert_equal(1, Cali.count)
macros = [:has_one, :has_many, :belongs_to, :has_and_belongs_to_many, :embeds_one, :embeds_many, :embedded_in]
puts "Cali.reflect_on_all_associations(*macros):#{Cali.reflect_on_all_associations(*macros).inspect}"
puts "Address.reflect_on_all_associations(*macros):#{Address.reflect_on_all_associations(*macros).inspect}"
#puts "Old API - Cali.associations:#{Cali.associations}"
#puts "Old API - Address.associations:#{Address.associations}"
end
end
テスト出力
Run options: --name=test_mongoid_fields
# Running tests:
Cali.reflect_on_all_associations(*macros):[#<Mongoid::Relations::Metadata
class_name: Address,
cyclic: No,
dependent: None,
inverse_of: N/A,
key: address,
macro: embeds_one,
name: address,
order: nil,
polymorphic: No,
relation: Mongoid::Relations::Embedded::One,
setter: address=,
versioned: No>
]
Address.reflect_on_all_associations(*macros):[#<Mongoid::Relations::Metadata
class_name: Cali,
cyclic: No,
dependent: None,
inverse_of: address,
key: cali,
macro: embedded_in,
name: cali,
order: nil,
polymorphic: No,
relation: Mongoid::Relations::Embedded::In,
setter: cali=,
versioned: No>
]
.
Finished tests in 0.008605s, 116.2115 tests/s, 116.2115 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips