Rails - display category name on views via relationship tables












2















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?










share|improve this question























  • If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

    – Marc Solva
    Jan 2 at 6:17
















2















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?










share|improve this question























  • If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

    – Marc Solva
    Jan 2 at 6:17














2












2








2


1






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?










share|improve this question














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?







ruby-on-rails






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 5:23









Marc SolvaMarc Solva

283410




283410













  • If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

    – Marc Solva
    Jan 2 at 6:17



















  • If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

    – Marc Solva
    Jan 2 at 6:17

















If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

– Marc Solva
Jan 2 at 6:17





If I used this <td><%= blog.post_category %></td> it gives me something like this: #<PostCategory:0x007fafd058f070>

– Marc Solva
Jan 2 at 6:17












2 Answers
2






active

oldest

votes


















2














As per you schema provided, it is noted that PostCategory object has many Blog objects



model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<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 %>





share|improve this answer


























  • RAY Tried this already but got undefined method `name' for nil:NilClass

    – Marc Solva
    Jan 2 at 6:18













  • updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

    – ray
    Jan 2 at 6:19











  • @MarcSolva for blog.post_category_id, there must be id present in table post_categories

    – ray
    Jan 2 at 6:21











  • It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

    – Marc Solva
    Jan 2 at 6:22






  • 1





    @ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

    – Marcin Kołodziej
    Jan 2 at 9:45



















1














model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<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 %>





share|improve this answer


























  • PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

    – Marc Solva
    Jan 2 at 6:03











  • First reset your database and start creating

    – Cryptex Technologies
    Jan 2 at 6:05











  • Why you are calling post_categories.blog_id

    – Cryptex Technologies
    Jan 2 at 6:06











  • Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

    – ray
    Jan 2 at 6:12











  • belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

    – Marc Solva
    Jan 2 at 6:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001594%2frails-display-category-name-on-views-via-relationship-tables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














As per you schema provided, it is noted that PostCategory object has many Blog objects



model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<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 %>





share|improve this answer


























  • RAY Tried this already but got undefined method `name' for nil:NilClass

    – Marc Solva
    Jan 2 at 6:18













  • updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

    – ray
    Jan 2 at 6:19











  • @MarcSolva for blog.post_category_id, there must be id present in table post_categories

    – ray
    Jan 2 at 6:21











  • It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

    – Marc Solva
    Jan 2 at 6:22






  • 1





    @ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

    – Marcin Kołodziej
    Jan 2 at 9:45
















2














As per you schema provided, it is noted that PostCategory object has many Blog objects



model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<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 %>





share|improve this answer


























  • RAY Tried this already but got undefined method `name' for nil:NilClass

    – Marc Solva
    Jan 2 at 6:18













  • updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

    – ray
    Jan 2 at 6:19











  • @MarcSolva for blog.post_category_id, there must be id present in table post_categories

    – ray
    Jan 2 at 6:21











  • It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

    – Marc Solva
    Jan 2 at 6:22






  • 1





    @ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

    – Marcin Kołodziej
    Jan 2 at 9:45














2












2








2







As per you schema provided, it is noted that PostCategory object has many Blog objects



model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<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 %>





share|improve this answer















As per you schema provided, it is noted that PostCategory object has many Blog objects



model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<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 %>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 6:25

























answered Jan 2 at 6:16









rayray

3,3561829




3,3561829













  • RAY Tried this already but got undefined method `name' for nil:NilClass

    – Marc Solva
    Jan 2 at 6:18













  • updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

    – ray
    Jan 2 at 6:19











  • @MarcSolva for blog.post_category_id, there must be id present in table post_categories

    – ray
    Jan 2 at 6:21











  • It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

    – Marc Solva
    Jan 2 at 6:22






  • 1





    @ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

    – Marcin Kołodziej
    Jan 2 at 9:45



















  • RAY Tried this already but got undefined method `name' for nil:NilClass

    – Marc Solva
    Jan 2 at 6:18













  • updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

    – ray
    Jan 2 at 6:19











  • @MarcSolva for blog.post_category_id, there must be id present in table post_categories

    – ray
    Jan 2 at 6:21











  • It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

    – Marc Solva
    Jan 2 at 6:22






  • 1





    @ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

    – Marcin Kołodziej
    Jan 2 at 9:45

















RAY Tried this already but got undefined method `name' for nil:NilClass

– Marc Solva
Jan 2 at 6:18







RAY Tried this already but got undefined method `name' for nil:NilClass

– Marc Solva
Jan 2 at 6:18















updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

– ray
Jan 2 at 6:19





updated answer, it is your data problem as association and schema is proper but you do not have data as per association. use of try method can handle issue

– ray
Jan 2 at 6:19













@MarcSolva for blog.post_category_id, there must be id present in table post_categories

– ray
Jan 2 at 6:21





@MarcSolva for blog.post_category_id, there must be id present in table post_categories

– ray
Jan 2 at 6:21













It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

– Marc Solva
Jan 2 at 6:22





It works but is it safe to just let "try" method handle the work? is this a good practice? Also can explain the association issue more?

– Marc Solva
Jan 2 at 6:22




1




1





@ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

– Marcin Kołodziej
Jan 2 at 9:45





@ray mitrev.net/ruby/2015/11/13/the-operator-in-ruby

– Marcin Kołodziej
Jan 2 at 9:45













1














model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<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 %>





share|improve this answer


























  • PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

    – Marc Solva
    Jan 2 at 6:03











  • First reset your database and start creating

    – Cryptex Technologies
    Jan 2 at 6:05











  • Why you are calling post_categories.blog_id

    – Cryptex Technologies
    Jan 2 at 6:06











  • Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

    – ray
    Jan 2 at 6:12











  • belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

    – Marc Solva
    Jan 2 at 6:15
















1














model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<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 %>





share|improve this answer


























  • PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

    – Marc Solva
    Jan 2 at 6:03











  • First reset your database and start creating

    – Cryptex Technologies
    Jan 2 at 6:05











  • Why you are calling post_categories.blog_id

    – Cryptex Technologies
    Jan 2 at 6:06











  • Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

    – ray
    Jan 2 at 6:12











  • belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

    – Marc Solva
    Jan 2 at 6:15














1












1








1







model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<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 %>





share|improve this answer















model changes,



class PostCategory < ApplicationRecord
has_many :blogs
end

class Blog < ApplicationRecord
belongs_to :post_category
end


view changes,



<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<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 %>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 6:36

























answered Jan 2 at 5:40









Cryptex TechnologiesCryptex Technologies

800213




800213













  • PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

    – Marc Solva
    Jan 2 at 6:03











  • First reset your database and start creating

    – Cryptex Technologies
    Jan 2 at 6:05











  • Why you are calling post_categories.blog_id

    – Cryptex Technologies
    Jan 2 at 6:06











  • Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

    – ray
    Jan 2 at 6:12











  • belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

    – Marc Solva
    Jan 2 at 6:15



















  • PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

    – Marc Solva
    Jan 2 at 6:03











  • First reset your database and start creating

    – Cryptex Technologies
    Jan 2 at 6:05











  • Why you are calling post_categories.blog_id

    – Cryptex Technologies
    Jan 2 at 6:06











  • Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

    – ray
    Jan 2 at 6:12











  • belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

    – Marc Solva
    Jan 2 at 6:15

















PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

– Marc Solva
Jan 2 at 6:03





PG::UndefinedColumn: ERROR: column post_categories.blog_id does not exist LINE 1: ... "post_categories".* FROM "post_categories" WHERE "post_cate...

– Marc Solva
Jan 2 at 6:03













First reset your database and start creating

– Cryptex Technologies
Jan 2 at 6:05





First reset your database and start creating

– Cryptex Technologies
Jan 2 at 6:05













Why you are calling post_categories.blog_id

– Cryptex Technologies
Jan 2 at 6:06





Why you are calling post_categories.blog_id

– Cryptex Technologies
Jan 2 at 6:06













Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

– ray
Jan 2 at 6:12





Downvoted! blog belongs_to :post_category as blogs table have foregin key post_category_id

– ray
Jan 2 at 6:12













belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

– Marc Solva
Jan 2 at 6:15





belongs_to :post_category as blogs place this on my blog model but I got this: compile error # GET /blogs.json def index @blogs = Blog.all end def post_category_0

– Marc Solva
Jan 2 at 6:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001594%2frails-display-category-name-on-views-via-relationship-tables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith