Model Releationship, belong, has?

Hello,

I have two models.

blog_post
belongs_to :blog_category

blog_category
has_many :blog_post

Whenever I do this, I get error (uninitialized constant
BlogPost::BlogCategories)
<%= post.blog_categories.name %>

Post is variable which continue one row from the blog_post table.

BUT when I write this, it works, but this is bad to have it in the view
:frowning:
<%= BlogCategory.find_by_id(post.blog_category_id).name %>

Anyone knows why I’m getting:
uninitialized constant BlogPost::BlogCategories?

and thanks many times :slight_smile:

Jamal S. wrote:

Hello,

I have two models.

blog_post
belongs_to :blog_category

blog_category
has_many :blog_post

Weird ?

Fixed,

it was because…

belong_to :blog_categories

should be

belong_to :blog_category

In this topic I just made a example and wrote it right :smiley:

Reference: http://snippets.dzone.com/posts/show/2089
Google: Rails+find+include

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs