Tag Archives: introductory

One-Shot Scaffolding Creation

If you’re not that familiar with Rails, you might be creating your application domain entities (models, controllers, business objects, whatever you want to call them) by generating the model, then generating the controller, then populating the fields into the migration, and the new/edit views. There’s a one-line way to do all of this, though. Continue reading

Posted in Development | Tagged , , | Comments Off

Private Member Variables in Ruby

How can you create private member variables in Ruby? If you’re used to the attr_accessor helper, that won’t work–that makes your member variables public. You can use the @ notation, eg. @variable_name. (Attr_accessor just gives you free getters and setters.) Continue reading

Posted in Development | Tagged , , , | 1 Comment

Exception and Error Handling

A lot of programming languages have an error-catching (or exception-handling) mechanism, where you can trap errors. How do we do this in Ruby? There are two keywords: begin, and rescue; and the ancestor of all errors is called StandardError. Continue reading

Posted in Development | Tagged , , | 1 Comment

Drilling Down into Validation Failure

Rails provides model-level validation, which is easy and DRY. But if you have a class that validates multiple fields, how can you verify if validation is failing on the right fields? ActiveRecord provides a convenient errors method with an invalid? method you can call; it takes a field name. Continue reading

Posted in Testing | Tagged , , | 1 Comment

Many-to-Many Association

How can you create a many-to-many association in Rails? We discuss two approaches. The first approach uses a join table with a composite primary key, and nothing else; Rails does the heavy lifting. The second approach is through an intermediary class that contains additional data about the relationship of the two elements in the many-to-many association. Continue reading

Posted in Development | Tagged , , | 1 Comment

Passing Controller/View Data with Instance Variables

In Rails, instance variables are preceded by the @ symbol. Instance variables in controllers are automatically accessible in view pages. In this way, Rails ties together the model, view, and controller aspects. The result is that you access model data and store it in instance variables in your controller, then display it through using those variables in your view code. Continue reading

Posted in Development | Tagged , , | Comments Off

Find_by_SQL

How can you use the find function if you have complex find criteria–such as searching by fields that don’t exist in the table for your class? Enter the find_by_sql function. It allows you to use arbitrary SQL to find the model records you want–everything from joins and where clauses to having clauses. Rails handles the nitty-gritty work of returning models that have the attributes you selected. Continue reading

Posted in Development | Tagged , , , | Comments Off

Migrations

Migrations allow you to write database-level code in a platform-independent way. You can create, edit, and delete tables, even populate data–all from migrations. Additionally, migrations allow you to iteratively modify production databases without worrying about destroying them. Migrations are reversible, and follow a numbering system. Continue reading

Posted in Development | Tagged , , | Comments Off

Date and Time Fields in Object Forms

How can you create a date or datetime field in Rails, quickly and easily, for your form objects? Rails has two helpers–date_select, and datetime_select. Both take a model instance name and a field name; they generate multiple fields required for precise selection of date and time values. Continue reading

Posted in Development | Tagged , | Comments Off

Routing in Rails

One aspect of web development developers would like control over is the URL structure. In most web development languages, the URL is intrinsically tied to the folder and file structure. Rails–although it also allows use of this structure for static … Continue reading

Posted in Development | Tagged , , , | Comments Off