# Basic Rails... one-to-many and "filtering" using foreign key

**URL:** <https://www.ruby-forum.com/t/basic-rails-one-to-many-and-filtering-using-foreign-key/126799>\
**Category:** Rails\
**Created:** [January 22, 2008, 10:27am UTC](https://www.ruby-forum.com/t/basic-rails-one-to-many-and-filtering-using-foreign-key/126799 "2008-01-22T10:27:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcHoeppner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@MarcHoeppner](https://www.ruby-forum.com/u/MarcHoeppner)\
**Post date:** [January 22, 2008, 10:27am UTC](https://www.ruby-forum.com/t/basic-rails-one-to-many-and-filtering-using-foreign-key/126799/1 "2008-01-22T10:27:50Z")

</div>

Hi everyone,

my apologies in advance since this is a rather trivial question, I  
believe. But although I have the Pragmatic Programmers book laying on  
my desk, I cant quite find a clear answer to the following question:

I have two tables - genes and organisms (therefore the models gene and  
organism). Using the normal procedure, I created both models and a  
controller (interface\_controller) for my application.  
The genes table holds information on genes, using ‘id’, ‘organism\_id’,  
and some other information. Obviously, an organism has many genes. I  
thus created “belongs\_to” in the gene model and “has\_many” in the  
organism model. genes.organism\_id references to organisms.id in the  
database.

The main page displays a list of organisms in the database, but what I  
have failed to figure out thus far is the following:

How do I link an organism to all its genes (via organism\_id). Meaning:  
How do I pass the organism\_id to a method in the controller and  
thereby deciding which genes are then shown in a new page  
(list\_genes.rhtml). I hope that is kind of clear 😉 I believe I have  
to write a method in the controller “def list-genes”, but I dont know  
quite how.  
Any advice would be greatly appreciated.

Cheers,  
Marc

---

<div class="post-metadata">

**Author:** ![MarcHoeppner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@MarcHoeppner](https://www.ruby-forum.com/u/MarcHoeppner)\
**Post date:** [January 22, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/basic-rails-one-to-many-and-filtering-using-foreign-key/126799/2 "2008-01-22T18:52:10Z")

</div>

pass @genes from your controller. you can fill it like this.

@organism = Organism.find(params[:id])  
@genes = @organism.genes

so in your view you can do…

\<% for gene in @genes %\>  
\<%= g.name %\> blah blah.

\<% end %\>
