I have a schema for a relational database, which I’d like to generate scaffolding for in my Ruby on Rails 3.2.8 project. I’ve found the documentation pretty confusing though, and my efforts so far have failed, so my question is exactly how I’d go about generating the needed scaffolding/models for the following schema:
USER
name:string
email:string
-----
has_many: posts
TAG
name:string
-----
belongs_to_and_has_many: series
belongs_to_and_has_many: posts
POST
title:string
body:text
-----
belongs_to_and_has_many: tags
belongs_to: user
belongs_to: category
SERIES
name:string
website:string
-----
belongs_to_and_has_many: tags
CATEGORY
name:string
-----
has_many: posts

Here we go :
Here I’m using
referencesbecause it has the added benefit of automatically generating an index on the column for you. Although the DB generation is nice, I suggest you review the actual db/migrations files generated and add more indexes etc …If you don’t want indexes by default, just generate using
model_name_id:integerlikeserie_id:integer(Note the singular form here)I’d also consider renaming your association tables to be singular :
TagPostandTagSeriesince it’s an association between aTagand either aSerieor aPostAlso beware, when generating automatic rails compliant foreign keys, they’ll by default be named
model_name_idand notid_usersas your schema mentions. All of this can be changed but it’s easier to make Rails figure out everything for you. Convention over configuration is one of the big strength of Rails.