Author: admin

  • The first classic exercise is to build a blog using RoR

    To build a blog using Ruby on Rails, you will need to do the following: Install Ruby and Rails Make sure you have Ruby and Rails installed on your system. You can check if you already have Ruby installed by running the command ruby -v in your terminal. If you don’t have Ruby installed, you…

  • Properly Managing Dates, Times, and Timezones

    While Rails offers a number of useful classes and methods for manipulation of dates and times, it is not uncommon for developers to run into inconsistencies and issues when timezones begin to enter into the mix.  Even for applications developed without a requirement of timezone support, often an application is actually dealing with a multitude…

  • Robust Models and Tiny Controllers

    It is typically the best practice in Rails to keep the majority of your business logic and execution within your models, and thus away from your controllers.  That isn’t to say the paradigm of developing “fat models” is necessarily appropriate in all cases, and of course, your design pattern should be based on the needs…

  • Utilize Active Record Validations

    All applications — particularly web applications that accept user data — need a solid foundation of data validation, and directly within the model is a great place to perform these initial validations.  Proper validation will ensure that only valid data is processed and ultimately saved to your database. Rails offers plenty of built-in validation helpers, or you can…

  • Generating Initial Database Data

    While most data during development can be generated on the fly during your testing procedures, you may often find yourself in a situation where you need specific dummy data to fill up your database while you work and tweak your code or even data for the live environment. Luckily, the db/seeds.rb file exists exactly for this purpose.…

  • Properly Associating Many to Man

    During development, you may find you need to associate two models through a many-to-many relationship, whereby each instance of a model can be matched with any number of instances of the other model, and vice versa. There are essentially two methods by which to create such an association, so we’ll briefly discuss both options and…

  • Jump Start with Scaffolding

    Scaffolding is a great feature of Rails for individuals new to the development, or perhaps those just new to Rails itself. Scaffolding is one of the many generate commands that allows you to automatically create all the framework files you need for a simple object, including the model, controller, database migration file, and basic views. For…

  • Creating Self-Contained Ruby

    Putting Ruby code in a file is obviously far more convenient and practical than using multiple -e command-line options. However, suppose we want to go a step further and be able to run a Ruby-based program by typing the file name containing the code rather than the ruby command. On Linux or UNIX, this is…

  • Executing Ruby File from Terminal

    We will be creating a ruby file in VSCode with the name ‘SimpleExample.rb’. SimpleExample.rb: You can also try this code with Online Ruby Compiler Run Code We will now execute the ruby file from the terminal using the command “ruby <filename>”.

  • Interactive Execution of Ruby

    Ruby is an interpreted programming language. This basically means that Ruby source code is not pre-compiled like it is with languages like C or C++, but rather is compiled and executed at run time. One advantage of being an interpreted language is that we can type Ruby code directly into the interpreter and have it…