How to model a comment system

Asked

Viewed 108 times

1

I am developing an application in Rails. I would like to know how to model the comments part. There is a table for the calls( Helpdesk system ), and the same relates to the history table, which are the comments. My question is, how can I make sure I find out who made a comment, because the call table relates to the call table_employees, and to the users table, and I need to filter who commented.

  • You already have part of the structure ready by what you described. I’m going to put the image of a simple structure: http://postimg.org/image/ejjvte03r/ There are four tables: called / history / user / called_historico, called "link" the history the calls, and with the history id you can retrieve the user later. It’s just the idea, lack implementation, because I don’t know how is the YOUR current structure. I hope that me have correctly understood your need too.

1 answer

1

Your history table also needs to have the id of the User who made the comment. After that, add a belongs_to :usuario (for example).

To list the table chamada with the table usuario, using the table chamada_funcionarios to relate them, for example, you need to add this relation in the model Chamada (has_one :usuario, through: :chamada_funcionarios).

Once these relations have been made, you can consult the author of the call by the model Chamada and the author of the comments by the model Historico.

For example:

chamada = Chamada.first
comentarios = chamada.historicos
puts "Chamada #{chamada.id}:"
puts "Autor: #{chamada.usuario.nome}"

comentarios.each do |comentario|
  puts "Comentário #{comentario.id}"
  puts "Autor do comentário: #{comentario.usuario.nome}"
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.