Here is my controller action
@posts = Post.where(sub_category_id: params[:id]).paginate(page: params[:page], per_page: 2).to_a
The post table contains category_id,sub_category_id,product_id. All there are three tables
Now i need the mapping name from the id. how can i achieve it.
I tried @sub_category = SubCategory.find params[:id]
to get sub category it works fine.
Whether above method is good? If yes how can i achieve it to get other table datas.
Edit-1
I am having table post with following fields
- id
- category_id
- sub_category_id
- product_id
- title
I am having following set of tables
- category
- sub category
- product
In my view my title
is hyperlink. When i click on it. It will redirect to respected post page and i have to show a breadcrum
like this Electronics->Laptop->Dell
Edit-2
I tried it but getting error as
undefined method `category_id' for #<ActiveRecord::Relation::ActiveRecord_Relation_Post:0xb4e7a9f0>
Tried the following
@posts = Post.where(sub_category_id: params[:id]).paginate(page: params[:page], per_page: 2).to_a
@category = Category.find(@posts.category_id) unless @posts.blank?