title: How can I filter data on a rails index page using columns from a linked table?
body: <p>I am looking for help with adding a search filter to an existing index page that normally shows all families in my geneaology database.</p>
  <p>The family table holds zero or more father_ids and zero or more mother_ids that point to the Persons table which describes a person.</p>
  <p>Here is the relevant code in the families controller (simplified until I get it to work).</p>
  <pre><code>  def index
      if params[:search]
        @families = Family.where(&quot;Person.find('fam_fatherid').first_name like 'bob%'&quot;) 
      else
        @families = Family.all.order(params[:sort])
      end
  </code></pre>
  <p>This results in a syntax error.  I have also tried several other ways to access the data, but to no avail.
  Example : @families = Family.where (&quot;fam_fatherid.firstname like 'bob%'&quot;).  This doesn't work because fam_fatherid is an integer and not an object (I assume). The error I get is:
  SQLite3::SQLException: no such column: fam_fatherid.first_name</p>
  <p>FWIW, here is the schema for the people table, just to verify that first_name is a valid column.</p>
  <pre><code>CREATE TABLE &quot;people&quot; (&quot;id&quot; integer PRIMARY KEY AUTOINCREMENT NOT NULL, &quot;name_prefix&quot; varchar, &quot;first_name&quot; varchar, &quot;middle_name&quot; varchar, &quot;last_name&quot; varchar, &quot;name_suffix&quot; varchar, &quot;date_of_birth&quot; varchar, &quot;date_of_death&quot; varchar, &quot;gender&quot; varchar, &quot;notes&quot; varchar, &quot;sync_outlook&quot; varchar, &quot;sync_phone&quot; varchar, &quot;flags&quot; varchar, &quot;created_at&quot; datetime(6) NOT NULL, &quot;updated_at&quot; datetime(6) NOT NULL, &quot;place_of_birth&quot; varchar, &quot;aliases&quot; varchar);
  </code></pre>
  <p>This is the model:</p>
  <pre><code>class Family &lt; ApplicationRecord
      has_many :childlinks, foreign_key: &quot;child_family&quot;
      has_many :people, through: :childlinks
  
      # attempting to link parents
      has_many :people, foreign_key: &quot;fam_fatherid&quot;
      has_many :people, foreign_key: &quot;fam_motherid&quot;
  end
  </code></pre>
  <p>If someone can just point me in the right direction, I would appreciate it.  I just started with Rails a few months ago, and I don't mind doing the research, but so far, I have spent many hours and just seem to be going in circles. I've been able to add a search to the people index page because the first_name (etc.) is a column in the Persons table and not a link to another table.
  Thanks in advance, and I hope I've provided enough information.
  Jere</p>