 |
April 2007 Archives
Rails and Model Relationships
Posted by bleonard on April 27, 2007 at 05:33 PM | Permalink
| Comments (1)
Here we enhance our Ruby web log so that readers can add comments. In the process we will learn how to deal with model relationships in Rails, as a single post can have zero or more comments.
Setting Things Up
I'm going to begin from where I left off in my previous post: Rails and Database Migrations. Alternatively, you can start from BlogDemo.zip, which is the completed project from that post.
Test the BlogDemo Project
- Open the BlogDemo project.
- Run the project and browse to http://localhost:3000/blog to verify it works.
Create the Comment Model
- Switch back to the Projects tab, right-click the Models folder and generate a new model named Comment.
- Open 003_create_comments.rb and add post_id, created_at and comment columns to the up method as follows:

- Right-click the BlogDemo project and select Migrate Database > To Current Version
Define the Model Relationships
- Add the has_many association to post.rb:
- Add the belongs_to association to comment.rb:
Regenerate our Controller Scaffolding
When we first created this project and generated the scaffolding for the Post model, we didn't choose to overwrite the existing blog controller, so the scaffolding actions were not created. So that we can customize the scaffold actions, we will delete the blog_controller.rb and generate the scaffolding again. Note, we are choosing not to use the overwrite option because our views have been customized and we we don't want to loose those customizations:
- Delete blog_controller.rb
- Generate Scaffold. Set the Model Name to Post and the Controller Name to Blog.
- Open the newly created blog_controller.rb, which now has all of the scaffold actions (index, list, show, new, create, edit, update and destroy).
Create a New Action for Posting Comments
We're going to modify the show view to include a text area so comments can be added. First we're going to create a new action for posting the comments.
- Modify the show action to save the post id to the flash:

- Create the following post_comment action:

Modify the View to Post Comments
- Open show.rhtml and add the following to the bottom:

Test
- In the browser, click one of the Permalinks to view an entry's details. Test adding a comment:

Displaying the Comments
Our blog doesn't yet display the comments that have been added, let's fix that.
- Add a post_comments instance variable to the show action to collect the comments:
- Add the following to show.rhtml to display the comments. The <hr> and <h4>Comments</h4> already exist and are provided for placement reference.
Test
- Add another comment:
The Completed Application
BlogDemoComments.zip
Rails and Database Migrations
Posted by bleonard on April 05, 2007 at 02:58 PM | Permalink
| Comments (2)
After my first Ruby blog entry, David Rupp commented that I should do certain things the more conventional way, like using database migrations rather then SQL scripts. So here's a reprise of that entry, doing just that plus a couple of other minor improvements.
Setting Things Up This time around I'm going to use Instant Rails. All of the instructions for setting up Instant Rails and NetBeans are documented here. However, these steps also work with the default JRuby configuration that comes with the Ruby module.
If you already have the Ruby module installed, check the Update Center for a newer version as it's updated almost daily. At the time of this writing it's up to version 0.54:

Create the Ruby Project
- Create a new Ruby On Rails Application named BlogDemo. Wait while the project is generated and the Mongrel Web Server is started.

Create a Database
- Right-click your mysql cookbook connection (if you don't have one, refer to here) and select Execute command. Enter:
- Right-click the MySQL (Connector/J driver) and select Connect Using. Enter jdbc:mysql://localhost/BlogDemo_development as the Database URL and root as the User Name.
Create the Model
- Switch back to the Projects tab, right-click the Models folder and generate a new model named Post. A migration is created for you as part of the model generation:
- Open 001_create_posts.rb and add the title column to the up method as follows:

- Right-click the BlogDemo project and select Run Rake Target > db > migrate.
- Switch to the Runtime tab to see the generated posts table and associated schema_info table.
Using a Scaffold
One of Rails' strengths is how easy it is to test your models. Check this out:
- Right-click the Controllers folder and generate a controller named Blog.
- Add the following to BlogController, which will provide a basic CRUD application around our model:

- Open routes.rb found in the Configuration folder and add the following:

- Delete index.html from the Public folder. Now our blog will load automatically when we run the application.
- Press F6 to start the Mogrel server and launch the browser.


Migrating Forward
- Right-click the Database Migrations folder and select Generate. Name the migration AddBody. Ruby will create a versioned migration script.
- Open 002_add_body.rb and modify it as follows:

- Right-click the BlogDemo project and select Run Rake Target > db > migrate.
- Return to the browser and click on the Edit link to see how Ruby has recognized our new body field.
- Note, support for migrating to a specific version is still being worked on. You can track the progress of that issue here. For now, you can do it from the command line. For example, to migrate back to version 1 of the schema, open the command prompt at the project's root and run rake db:migrate version=1

If you do this, don't forget to migrate back to version 2 before continuing:

- Add a few more entries, for example:
Make the List Look More Like a Blog
This far, scaffolding has created a basic CRUD application that allows us to easily test our model. Here we will generate scaffolding that we can then use as a base for modifying our user interface.
- Right-click the Views folder and generate a scaffold referring to Post model and the Blog controller.
- Open list.rhtml and delete the entire table.
- In it's place, add the following:

- Save list.rhtml and refresh your browser.
- To display more like a typical blog, reverse the sort order by adding .reverse to the end of @posts in list.rhtml:

The Completed Application
BlogDemo.zip
|