简体   繁体   English

Rails / STI-通用模型路径

[英]Rails/STI - common model path

I'm trying to learn how STI works and I have my Content model set up with Post and Article inheriting from it. 我正在尝试了解STI的工作方式,并且设置了我的内容模型,并从中继承了Post和Article。

I also have a Comment model. 我也有一个Comment模型。 In the comments controller, I have this: 在注释控制器中,我有以下内容:

def find_content
  @content = Content.find(params[:content_id])
end

Obviously, it isn't working since the params show either post_id or article_id. 显然,由于参数显示post_id或article_id,因此无法正常工作。 How do I tell the comments controller to pull in the relevant content_id based on the params? 如何告诉注释控制器根据参数引入相关的content_id?

When you use single table inheritance you get one and only one table with the column "type" in it. 当您使用单表继承时,您将获得一个且只有一个带有“类型”列的表。 So, in your case you have table "contents" with column "type" in which you store one of the strings: "post" or "article". 因此,在您的情况下,您具有表“ contents”和“ type”列,在其中存储以下字符串之一:“ post”或“ article”。

When you use STI in your Rails application you have to define three models: Content (inherit from ActiveRecord::Base as a regular model), Post and Article (both inherit from Content ) and operate on them directly, eg: Post.find , Article.last . 在Rails应用程序中使用STI时,必须定义三个模型:Content(作为常规模型从ActiveRecord::Base继承),Post和Article(均从Content继承)并直接对其进行操作,例如: Post.findArticle.last

Because both Post and Article are the rows of the same table "contents" each of them always has a distinct id. 由于“发布”和“文章”都是同一表“内容”的行,因此它们中的每一个始终具有不同的ID。 I do not know the logic of your application and what goals you are trying to achieve (for example, I can't understand what should be the logical difference between Post and Article), but I suggest to implement this action from your example as: 我不知道您的应用程序的逻辑以及您要实现的目标(例如,我无法理解Post和Article之间的逻辑区别),但是我建议从您的示例中执行以下操作:

def find_content
  @content = Post.find_by_id(params[:content_id]) || Article.find_by_id(params[:content_id]
end

I use .find_by_id not .find because the second will raise an exception you'll need to handle/rescue and the first will return nil . 我使用.find_by_id而不是.find因为第二个将引发您需要处理/救援的异常,第一个将返回nil

Of course, my example is not elegant, but I hope you'll get the point. 当然,我的例子并不优雅,但是希望您能理解。

A quick fix could be: 快速修复方法可能是:

@content = Content.find(params[:post_id] || params[:article_id])

In your parameters should be either an article_id or a post_id . 在您的参数中应该是article_idpost_id So you can try to catch both parameters and combine them with a logical or operator ( || ). 因此,您可以尝试捕获两个参数,并将它们与逻辑或运算符( || )组合在一起。 So you get an article_id or a post_id which you can use with Content.find . 因此,您将获得可以与Content.find使用的article_id或post_id。

params
# => {:article_id=>1, :other_parameter=>"value"}

params[:post_id] || params[:article_id]
# => 1

If there is neither of the parameters you get nil and Content.find fails with a ActiveRecord::RecordNotFound exception. 如果两个参数都不为nil ,则Content.find失败,并出现ActiveRecord::RecordNotFound异常。 That's ok, I think. 没关系,我想。 Later you will rescue this error in your controller to show a 404 page . 稍后,您将在控制器中解决此错误,以显示404页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM