Reputation: 1348
I'm trying to learn how STI works and I have my Content model set up with Post and Article inheriting from it.
I also have a Comment model. 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. How do I tell the comments controller to pull in the relevant content_id based on the params?
Upvotes: 0
Views: 127
Reputation: 461
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.
Upvotes: 1
Reputation: 3310
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
. 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
.
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. That's ok, I think. Later you will rescue this error in your controller to show a 404 page .
Upvotes: 1