Creating Scaffold Ruby on Rails

Asked

Viewed 109 times

0

Hi I’m new to Ruby on Rails. I’m trying to create a small blog site. I have two tables one for posts and one for comments. Each Post will have many comments, and third table will be the categories wanted to know how to relate the category table to the posts, for each post has 1 category. I’m using this command:

rails g scaffold Category name:string post:belongs_to

But the error that returns me when I ask this table is:

(1 error prohibited this Category from being saved:)

If anyone can help I’d be very grateful.

1 answer

0

Your question has two points I would like to comment on:

  • The first is in relation to the framework. To generate the relationship via scafold, use model: References. In that case you would generate the scaffold as follows:

    Rails g scaffold Category name:string post:Ferences

The above command adds the Posts key in Category

And in Category.Rb would add belongs_to :post

  • The second point is and relation to modeling. Of course each application has its own business rule, but I think in your case it would be better to do the opposite, that is, a category have several posts, adding the category key in the Posts template.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.