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".
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, e.g.: Post.find
, Article.last
.
Because both Post and Article are the rows of the same table "contents" each of them always has a distinct 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:
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
.
Of course, my example is not elegant, but I hope you'll get the point.