MarcHoeppner
2008-Jan-22 09:27 UTC
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 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thatcher.craig-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-22 17:51 UTC
Re: Basic Rails... one-to-many and "filtering" using foreign key
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 %> On Jan 22, 4:27 am, MarcHoeppner <m...-EakUmDV/mUUVQbUkktcQLw@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---