Agile Web Development with Rails

Start from the beginning
                                        

Theadd_to_cart methodhandlesuserrequests.Inthiscaseit .ndsthecurrent user�fs shopping cart(whichis an object managedby the model).It also asks the model to .nd the information for product 123. It then tells the shopping cartto addthatproducttoitself.(Seehow the modelisbeing usedtokeep track of all thebusinessdata; the controller tellsit what todo, and the model knows how todoit.)

Now that the cart includes the new product, we can show it to the user. The controller arranges things so that the view has access to the cart object from the model, and it invokes the view code. In Rails, this invocation is often implicit; again conventionshelplink aparticular view with agiven action.

That�fs all there is to an MVC web application. By following a set of conven-tions and partitioning your functionality appropriately, you�fll discover that your codebecomes easierto workwith andyour applicationbecomes easierto extend and maintain.Seemslike agood trade.

If MVC is simply a question of partitioning your code a particular way, you might be wondering why you need a framework such as Ruby on Rails. The answeris straightforward:Railshandles all of thelow-levelhousekeepingfor you.allthose messydetailsthattakesolong tohandlebyyourself.andlets you concentrate onyour application�fs corefunctionality.Let�fs seehow....

2.2 Active Record: Rails Model Support

Ingeneral, we�fllwant our web applicationstokeep theirinformationin a rela-tional database. Order-entry systems will store orders, line items, and cus-tomerdetailsindatabasetables.Even applicationsthatnormally use unstruc-turedtext, such as weblogs and news sites, often usedatabases as theirback-enddata store.

Although it might not be immediately apparent from the SQL2 you use to access them, relationaldatabases are actuallydesigned around mathematical set theory. Although this is good from a conceptual point of view, it makes it dif.cult to combine relational databases with object-oriented programming

2. SQL, referred to by some as Structured Query Language, is the language used to query and update relational databases.

Report erratum

Prepared exclusively for Jordan A. Fowler

languages. Objects are all about data and operations, and databases are all about sets of values. Operations that are easy to express in relational terms are sometimesdif.cult to codein anOO system.The reverseis also true.

Over time, folks have worked out ways of reconciling the relational and OO views of their corporatedata.Let�fslook at twodifferent approaches.One orga-nizes your program around the database; the other organizes the database aroundyourprogram.

Database-centric Programming

The .rst folks who coded against relational databases programmed in proce-dural languages such as C and COBOL. These folks typically embedded SQL directlyintotheir code, either as stringsorby using apreprocessorthat con-vertedSQLin their sourceintolower-level calls to thedatabase engine.

The integration meant thatit became natural to intertwine the databaselogic with the overall application logic. A developer who wanted to scan through orders and update the sales tax in each order might write something exceed-ingly ugly, such as

EXEC SQL BEGIN DECLARE SECTION;

int id;

float amount;

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE c1 AS CURSOR FOR select id, amount from orders;

while (1) {

float tax;

EXEC SQL WHENEVER NOT FOUND DO break;

EXEC SQL FETCH c1 INTO :id, :amount;

You've reached the end of published parts.

⏰ Last updated: Mar 22, 2008 ⏰

Add this story to your Library to get notified about new parts!

Agile Web Development with RailsWhere stories live. Discover now