Tag Archives: framework

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

Foxy Fixtures Explained

Rails integrated a plugin called Foxy Fixtures into the core API. How does it work? It allows you to refer to other fixtures by name, instead of by ID (so no more hard-coded IDs). But there’s a gotcha–the IDs are generated automatically, so they’re not 1 or 2 like you might expect (for your first and second fixtures). Continue reading

Posted in Gems and Plugins, Testing | Tagged , , | 1 Comment

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

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

Database Independence

Rails allows you to abstract your code away from the underlying database platform through two facilities: migrations, which allow you to create, manipulate, and populate tables through code; and object-relational (O/R) mapping, which allows you to deal with objects and relationships, not tables. Continue reading

Posted in Development | Tagged , | Comments Off

Redirecting in Rails

Rails provides a convenient function called redirect_to which you can use to redirect the browser–no Javascript or meta-tag required! You can redirect to an action (even specifying parameters like an ID), or to the previous page, or to a relative or absolute URL. Continue reading

Posted in Development | Tagged , | 3 Comments

Undefined Method Foo

Sometimes, you add an attribute to a class, but when you try and access it, it tells you that the method is undefined. What’s going on? In this article, we discuss a bit about how Rails searches for things (database and code). The answer is probably that you forgot the getters and setters for that attribute! Continue reading

Posted in Development | Tagged , , , , | Comments Off

Validation 101

Data validation is crucial to web applications–especially e-commerce applications, where money is involved. Bad data can destroy the usefulness of (or even shut down) an application! What’s more, validation needs to be centralized–you want to keep your validation in one … Continue reading

Posted in Development | Tagged , , , , | 1 Comment

Created_On and Updated_On

Rails allows two special fields, created_on and updated_on, that it updates automatically. It tracks when model objects are created and updated. All you need to do is create the fields; Rails populates the values. Since these fields are useful, you can format them, to display them any way you please! Continue reading

Posted in Development | Tagged , , , | Comments Off