Tag: ruby on rails web development

Transactional callbacks in Ruby on Rails

There are two additional callbacks that are fired when a database transaction completes: after_commit and after_rollback. These callbacks are very similar to the after_save callback, except that they are not fired until the database changes have been committed or reversed. They are most useful when your Active Record models need to interact with external systems […]

Modifying Existing Migrations in Ruby

Periodically, you will make mistakes when writing migrations. If you’ve already run a migration, you can’t just edit the migration and run it again: Rails will assume it has already run a migration and will do nothing when you run rails db:migrate. You must rollback the migration (using bin/rails db:rollback, for example), edit the migration, […]

Using reversible in Ruby

A complex migration may involve processes that Active Record does not know how to reverse. You can use reversible to specify what to do when a migration is run and when it needs to be rolled back. For example: class ExampleMigration < ActiveRecord::Migration   def change     create_table :distributors do |t|       t.string :zipcode     […]

Model Generators

The model and scaffold generators will generate migrations suitable for creating a new model. The migration will contain instructions for creating the appropriate table. If you tell Rails which columns you want, expressions to add those columns will also be generated. For example, running: $ bin/rails generate model Product name:string description:text will create a migration […]

Validations in Ruby

Active Record allows you to check the state of a model before it is written to the database. There are several methods that can be used to check your models and validate that an attribute’s value is non-empty, unique (not existing in the database), conforms to a certain format, and many more. Validation is a […]

Naming conventions in RoR development

By default, Active Record uses some naming convention to know how the relationship between models and database tables should be created. Rails pluralizes class names to find the appropriate database table. For example, for the Book class, create a database table named books. Rails’ pluralization mechanisms are very powerful, being able to pluralize (and singular) […]

Safety during RoR development

(Basic Authentication) Basic authentication If you publish your blog online, anyone can add, edit, and delete articles or delete comments. Rails provides a very simple HTTP authentication system that works well in this situation. In the ArticlesController we need a way to block access to various actions if the user is not authenticated. Here we […]

Displaying articles on Ruby on Rails website

We still need a way to display a list of all our articles, let’s make one. The route displayed with bin/rails routes is the following: articles GET /articles(.:format) articles#index Add an index action corresponding to this route inside the ArticlesController in the app/controllers/articles_controller.rb file. When we write an index action, it’s common practice to place […]