PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to display has_many in rails controller and template?

 October 21, 2022     has-many, ruby-on-rails     No comments   

Issue

class Reflection < ApplicationRecord
    has_many :comments
end


class Comment < ApplicationRecord
    belongs_to :reflection
end

I have an application where a reflection has any comments.

In the index view for reflections, I would like to show a count of the number of comments each reflection has and also display the comments for each reflection, but I'm not able to figure out how to do this.

I am trying to understand what code to include in Reflection index controller as well as in the template (index.html.erb for Reflection). Any advice?

I am able to Display the comments for an individual reflection using the code below in the Reflections controller, but perhaps there's a better way to do this in rails.

  def show
    @comments = Comment.where(reflection_id: @reflection.id)
  end

I tried 

  <tbody>
    <% @reflections.each do |reflection| %>
      <tr>
        <td><%= reflection.id %></td>
        <td><%= reflection.reflection %></td>
        <td><%= reflection.user_id %></td>
        <td>
//LOOPING THROUGH COMMENTS IN EACH REFLECTION
        <td><%= reflection.comments.each do |comment| %>
              <%= comment.comment %>, 
            <% end %>
        </td>
//END LOOPING THROUGH COMMENTS IN EACH REFLECTION
        </td>
        <td><%= link_to 'Show', reflection %></td>
        <td><%= link_to 'Edit', edit_reflection_path(reflection) %></td>
        <td><%= link_to 'Destroy', reflection, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>


The above yields the comment but also the objects afterwards:

    fdsafdsa, fdsacdxzdv, [#<Comment id: 8, comment: "fdsafdsa", created_at: "2019-08-27 04:13:34", updated_at: "2019-08-27 04:13:34", reflection_id: 1, user_id: 1>, #<Comment id: 9, comment: "fdsacdxzdv", created_at: "2019-08-27 04:32:36", updated_at: "2019-08-27 04:32:36", reflection_id: 1, user_id: 1>]  

Solution

In the controller to fetch the comments, you could use

@comments = @reflection.comments and then in view file to display the comments, you could loop through @comments.

To display the count of the number of comments in the view file you could use @comments.size

https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

To remove the comment object from being displayed, try the below. (I have removed the = before looping)

 <td><%reflection.comments.each do |comment| %>
              <%= comment.comment %>, 
            <% end %>
 </td>

For more info on rendering read ruby docs https://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html



Answered By - Luna Lovegood
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing