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

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 in the

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 :wink: 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.


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 %>
<%= %> blah blah.

<% end %>