I have two tables that I put relationship with blog and post_categories. Below is the schema:
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.integer "status", default: 0
t.bigint "post_category_id"
t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
end
create_table "post_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Now what I am trying to do is to display the category name instead of category id on my blog index.html.erb file. I did the following but did not work:
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<% PostCategory.all.each do |c| %>
<% if c.id == blog.post_category %>
<td><%= c.name %>
<% end %>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
Note: I also tried accessing it directly via <td><%= blog.post_category.name %></td>but did not work still.
Any idea how can I display the category name instead of category id associated?
<td><%= blog.post_category %></td>it gives me something like this:#<PostCategory:0x007fafd058f070>