DEV Community

arbarrington
arbarrington

Posted on • Updated on

Rails CLI Generation

Rails CLI Generation

Thank you Cassidy for the guidance below!

Image description

rails new application-name

rails g
-model: creates model and migration
-migration
-controller
-scaffold
-serializer: use singular for corresponding model. Dictates what attributes and json structure that will be returned
ActiveModel::Serializer provides some powerful yet simple-to-use tools for crafting the JSON our app returns, and it does so in a way that's consistent with Rails conventions.
To customize the JSON returned for a resource, create a serializer for that resource and list the desired attributes.
The serializer is used implicitly by Rails based on naming conventions; to override this, custom serializers can be passed explicitly.
AMS enables the use of the belongs_to and has_many macros in serializers to render associated data; these macros should be used sparingly.
By default, AMS will only nest associations one level deep in the serialized JSON. To override this, the include option can be used.
-resource: creates model, migration, controller and view files. Schema is updated/created. Controller will automatically generate 7 routes that follow REST.

rails d (destroy)

rails s (server)

rails db (similar to rake db commands)

rails c (console)

options
--no-test-framework

Nomenclature for models, controllers, and views
-Model names are always singular, and controller names are plural

class-name:belongs_to
updates association in model,
updates schema to
t.belongs_to :class-name, null: false, foreign_key: true

rails routes
A client sends a request to the server (this could be: a user entering a URL in a browser; a JavaScript application using fetch; etc)
That request is sent to the server where the application's router interprets the request and sends a message to the controller mapped to that route
The controller uses the model to access data from the database
The controller then uses that data to render a view (HTML or JSON)
The server returns an HTTP response, which contains the HTML or JSON data

Update a migration
-add a column
rails generate migration add_column-name_to_table-name(s) column-name:data-type
--api
--minimal

***Combine these command templates
npx create-react-app client --use-npm
rails new dvd-shop --api --minimal
rails g resource Movie title year:integer length:integer director description poster_url category discount:boolean female_director:boolean --no-test-framework

Adding foreign keys to join table using CLI
rails g migration AddTaxiToRides taxi:references

Top comments (0)