# Parent child relationships and searching

**URL:** <https://www.ruby-forum.com/t/parent-child-relationships-and-searching/113426>\
**Category:** Rails\
**Created:** [August 31, 2007, 12:00am UTC](https://www.ruby-forum.com/t/parent-child-relationships-and-searching/113426 "2007-08-31T00:00:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [August 31, 2007, 12:00am UTC](https://www.ruby-forum.com/t/parent-child-relationships-and-searching/113426/1 "2007-08-31T00:00:04Z")

</div>

I have a parent model object called team, and a child model called  
team\_aliases. Like this:

create\_table teams do |t|  
t.column :name, :string  
end

create\_table team\_aliases do |t|  
t.column :team\_id, :integer  
t.column :name, :string  
end

For example, there may be a team called: “New York Mets”, with aliases  
of “NY Mets”, “Mets”, etc.

I have defined the teams model so that it “has\_many :team\_aliases”, and  
the team\_aliases model acts\_as\_list with the scope :team\_id.

My input data is a list of competitions, where each competition has an  
associated list of team names ( not team\_ids ). Given a competition, the  
application needs to find the list of matching teams, which should  
generally be 2 or more.

I am trying to implement a find method on the team model that searches  
on both the team name and the alias names to find matching teams for the  
given list of names from a competition.

def self.find\_by\_names(names)

I have figured out a way to do this with find\_by\_sql, but I would really  
prefer to use ActiveRecord find utilities so that I do not have to use  
sql.

This seems like a fairly common pattern which I need to solve for  
several different models in addition to teams, and I’m wondering if  
there is a rails preferred way for performing this kind of find method  
when a model instance can be known by more than one name.

Thanks!
