Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:53:43+00:00 2026-06-15T21:53:43+00:00

I’m struggling with grasping the fundamentals behind writing model methods, which is I think

  • 0

I’m struggling with grasping the fundamentals behind writing model methods, which is I think what I need to do to get this to work. Basically, I have a project view that’s showing project_todos that belong to that project. Then nested under that I have project_todo_comments that belong to a project_todo. All in one view.

I’ve set up the associations so that project has_many project_todos, project_todo belongs_to project and has_many project_todo_comments. Then of course project_todo_comment belongs_to project_todo.

In the projects controller, I’m doing this:

  def show
    @project = Project.find(params[:id])

    # Display the form to create a new todo
    @project_todo = ProjectTodo.new

    # Create the instance variable to display the loop in the view
    @todos = @project.project_todos

    # Display the form to create a new comment
    @project_todo_comment = ProjectTodoComment.new

     # Create the instance variable to display the loop in the view
    @comments = @project.project_todo_comments

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @project }
    end
  end

So this is working to the point where I can add todos on a project and display them, and I can also add comments to project todos and display them too, all in that same view. BUT, I’m having difficulties getting the comments to display ONLY with the todos that they’re associated with. Right now, when I add a comment on any todo, every single todo then displays that comment.

In the view, here’s where I’m displaying the project_todos and nesting the form to add a comment, then displaying the comments:

 <% @todos.each do |todo| %>
    <p><%= todo.title %></p>

    <%= form_for(@project_todo_comment) do |f| %>
        <%= f.hidden_field :project_todo_id, :value => @project_todo.id %>
        <%= f.hidden_field :project_id, :value => @project.id %>
        <%= f.text_area :comment %>
        <%= f.submit 'Add Comment' %>
    <% end %>

    <% @comments.each do |comment| %>
        <%= comment.comment %>
    <% end %>

  <% end %>

I’ve been told that I need to write a model method to get this to work, but I’m still trying to understand those. Could anyone just reassure that I do in fact need to write a model method, and point me in a direction to learn about them? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:53:44+00:00Added an answer on June 15, 2026 at 9:53 pm

    In your controller you get all of the comments, but in an actual todo item you just want the comments for that item. This can be done with: todo.project_todo_comments
    The easiest way to solve:

    - <% @comments.each do |comment| %>
    -   <%= comment.comment %>
    - <% end %>
    
    + <% todo.project_todo_comments.each do |comment| %>
    +   <%= comment.comment %>
    + <% end %>
    

    You can remove the line from your controller:

    - @comments = @project.project_todo_comments
    

    However in your form (where you create a comment) you use the wrong ID for the todo item:

    - <%= f.hidden_field :project_todo_id, :value => @project_todo.id %>
    + <%= f.hidden_field :project_todo_id, :value => todo.id %>
    

    By the way, I would just name those models:
    Todo and Comment, there is no point using that long modelnames. You define those associations in the start of your model, so if someone would read it, he/she would see it instantly.

    For example see myself, I knew what was the purpose of your models still misused the namings, cause of too long classnames.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.