Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

undefined method `ncbi_ref_seq' for #<ActiveRecord::Associations::CollectionProxy []>


Generator has_many:results Result belongs_to:generator

I want to have a page whereby i can view all the generators and the results.

For example :This is from my generator/index.html

enter image description here

this is from result/index.html

enter image description here

what i want to have is to combine them together and view all the data.enter image description here

right now this is what i've changed but i get the error message undefined method for ncbi_ref_seq. ( ncbi_ref_seq is an attribute belonging to the class Result )

GeneratorController.rb

def index
    @generators = Generator.all(:include => [:results])
  end

Generator/index.html.erb

 <tbody>
     <% @generators.each do |generator| %>
     <tr>
        <td><%= generator.primer_length %></td>
        <td><%= generator.choice %></td>
        <td><%= generator.random_primer_generated %></td>
        <td><%= generator.c_primer %></td>
        <td><%= generator.results.ncbi_ref_seq %></td>
    </tr>
     <% end %>

Solution

  • Since a generator has many results. When you take generator.results it returns an Active Record Collection, so there would be several records of Results, so you can't just extra a ncbi_ref_seq.

    Either you have to loop through generator.results and output each ncbi_ref_seq like so

    <% for result in generator.results %>
      <%= result.ncbi_ref_seq %>
    <% end %>
    

    Or a generator has_one result.