class Parent < ApplicationRecord has_many :children end class Child < ApplicationRecord belongs_to :parent end そして、DBのデータが次のように登録されていたとする。 parent = Namihei child = Sazae child = Katsuo child = Wakame parent = Misae child = Shinnosuke child = Himawari この状況で以下のようなメソッドを作りたい。 require "test_helper" class ParentTest < ActiveSupport::TestCase test "zで検索" do # Sazaeが該当するのでNamiheiが返る parents = Parent.child