Category: Ruby on Rails Examples
-
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…