Linking notes to different objects in the database

At my work, we have judges who perform many tasks, for example, evaluate films or compare two pieces of text.

We are developing a new database to store all of our data (we already have data, but the database in it is pretty hacked), and I'm starting to create a Rails analysis application that will serve as a dashboard for these judgments. Tables will include things like judges, films, text, videos, text comparisons.

As part of the application, we want to be able to add comments or flag elements from these tables. For example, someone might want to add a comment to Judge 1 that says, โ€œThis judge is very incompatible,โ€ and add a comment to rating 2 saying โ€œThis rating is unexpected,โ€ or mark different types of films or texts to watch.

What is the best way to handle adding comments or flags to a database? For example, do we want to create a new comment table for each object (add JudgesComments, MoviesComments, TextComments, etc.)? Or do we want to have one comment table with columns (id, comment), [which, I think, will require that the identifiers of the entire database are globally unique in the database, and not unique only within their table]?

0
source share
3 answers

You must use polymorphic associations , so you will have one comment model and controller. In accordance with the excellent # 154 "Polymorphic Association" Railscast , adding commentable_type:stringand commentable_id:integera table comments, your code should look something like this:

# app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

# app/models/judge.rb
class Judge < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

# app/models/movie.rb
class Movie < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

# app/models/text.rb
class Text < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

# app/controllers/comments_controller.rb
def index
  @commentable = find_commentable
  @comments = @commentable.comments
end

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(params[:comment])
  if @comment.save
    flash[:notice] = "Successfully created comment."
    redirect_to :id => nil
  else
    render :action => 'new'
  end
end

private

def find_commentable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
  nil
end

in routes:

# config/routes.rb
map.resources :judges, :has_many => :comments
map.resources :movies, :has_many => :comments
map.resources :texts, :has_many => :comments

and in view:

<!-- app/views/comments/index.html.erb -->

<div id="comments">
<% for comment in @comments %>
  <div class="comment">
    <%=simple_format comment.content %>
  </div>
<% end %>
</div>

<h2>New Comment</h2>
<%= form_for [@commentable, Comment.new] do |f| %>
  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>
+3
source

alt text

+1
source

I worked in a system where there was one comment table, and a globally unique identifier for each entry in each table that you could write a comment on. The system worked fine, it wasnโ€™t easy, and it was easy for new people to see how it worked. Creating new entries in comment tables was slow using computer standards, but this was not a problem for users of the system.

+1
source

Source: https://habr.com/ru/post/1788844/


All Articles