ruby - Getting error :ActiveRecord_Associations_CollectionProxy -


club model there store student class information.

i have following associations:

    class club < activerecord::base        has_many :mcqs     end      class mcq < activerecord::base      #validation     validates :topic, presence: true,length: { minimum: 5 }     validates :club, presence: true      #association     belongs_to :user     belongs_to :club     end 

and here club controller-

class clubscontroller < applicationcontroller  def student_show_index     @club = current_user.clubs   end    def student_show_topic     @club = current_user.clubs     @mcq = @club.mcqs   end  end 

i want print mcq of particular club student.

here view show topic of mcq(student_show_topic).

    <%= render 'assessment/type' %> <h1>list of topics</h1> <div class="main-cont">     <div class="stream-cont">         <div class="stream-cont">         <% @mcq.each |f| %>             <div class="feed-cont-title all-header">                 <tr>                     <td><%= f.topic %></td>                 </tr>             </div>         <% end %>     </div> </div> 

it association problem. club model can't access mcq model.

here error messege.

nomethoderror in clubscontroller#student_show_topic undefined method `mcqs' #<club::activerecord_associations_collectionproxy:0x007fb031e0cf48> extracted source (around line #81):  79 def student_show_topic 80  @club = current_user.clubs 81  @mcq = @club.mcqs 82 end 83 84 end 

after use @mcq = @club.first.mcqs

it show mcq of club. in club table assign club a

  1. 9-physics
  2. 9-chemistry

so want when click on 9-physics topic show physics mcq topics , on.

look @club not single object collection of object, can not call mcqs @club.

but can call @club.first.mcqs


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -