将注释与数据库中的不同实体相关联 [英] Associating notes with different entities in a database

查看:135
本文介绍了将注释与数据库中的不同实体相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我们有法官执行各种任务,例如评分电影或比较两段文字。

At my job, we have judges perform a variety of tasks, e.g., rate movies or compare two pieces of text.

我们正在设计一个新的数据库来保存我们所有的数据(我们已经有一些数据,但是它的数据库是相当的黑客),我开始构建一个Rails分析应用程序,作为这些判断的仪表板。表格将包括法官,电影,文字,电影评分,文本比较等。

We're in the process of designing a new database to hold all our data (we have some data already, but the database it's in is pretty hack), and I'm starting to build a Rails analytics application that will serve as a dashboard on these judgments. Tables will include things like Judges, Movies, Text, MovieRatings, TextComparisons.

作为应用程序的一部分,我们希望能够从这些表中添加评论或标记项目。例如,有人可能想向法官1添加评论,说这个判断是非常不一致的,并向评级2添加了一个评论,说这个评级是意想不到的,或者标记不同类型的电影或文本进行审核。

As part of the application, we want to be able to add comments or flag items from these tables. For example, someone might want to add a comment to Judge 1 saying "This judge is very inconsistent" and add a comment to Rating 2 saying "This rating is unexpected", or flag different types of movies or texts for review.

处理向数据库添加注释或标记的最佳方式是什么?例如,我们要为每个实体创建一个新的注释表(添加一个JudgesComments,MoviesComments,TextComments等)?或者我们想要有一个带有(id,comment)列的单个注释表[我猜,这将需要整个数据库中的id在数据库中是全局唯一的,而不是唯一在表中]?

What is the best way to handle adding comments or flags to the database? For example, do we want to create a new Comment table for each entity (add a JudgesComments, MoviesComments, TextComments, etc.)? Or do we want to have a single Comments table with (id, comment) columns [which, I guess, would require ids throughout the database to be globally unique within the database, instead of unique only within its table]?

推荐答案

您应该使用多态关联,以便您具有单个注释模型和控制器。根据优秀的#154多形态协会Railscast ,添加 commentable_type:string commentable_id:整数到您的注释表,您的代码应该看起来像这样:

You should use polymorphic associations, so that you will have a single Comment model and controller. According to the excellent #154 "Polymorphic Association" Railscast, after adding a commentable_type:string and commentable_id:integer to your comments table, 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

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

在视图中:

<!-- 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 %>

这篇关于将注释与数据库中的不同实体相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆