Parent child relationships and searching

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs