Archive for the ‘rails’ Category

Rails Backwards Incompatible Change Coming to 2.3, Application is Now ApplicationController

Monday, November 17th, 2008

Just a head’s up to Ruby on Rails fans out there. DHH just checked in a very backwards incompatible change for the 2.3 branch. The root class for your controllers is now ApplicationController and not Application.

I’m hoping this patch is reverted and instead rolled out for 3.0.

Issues with Active Scaffold and Rails 2.1, Solved

Saturday, June 28th, 2008

If you are looking to upgrade to Rails 2.1 and you are using Active Scaffold, be aware it still has a few rough edges.

Make sure you install the Rails 2.1 branch of Active Scaffold:


git clone git://github.com/activescaffold/active_scaffold
cd active_scaffold
git branch -r (just lists the branches)
git checkout origin/rails-2.1

(Check out the full thread for Rails 2.1 and Active Scaffold compatibility)

Second, I had to patch vendor/plugins/active_scaffold/lib/extensions/generic_view_path.rb. On line 53, make it look like this:


if !@template.controller.is_a?(ActionMailer::Base) && @template.controller.class.uses_active_scaffold?

I had to add !@template.controller.is_a?(ActionMailer::Base) &&

All my tests are now passing!

I still love Active Scaffold, even though it’s a bit behind the times.

worldofresources.pdf (application/pdf Object)

Wednesday, June 28th, 2006

Resources on Rails highlights some of the new exciting changes to appear in Rails 1.2. The most exciting is ActiveResource, which CRUDifies models as HTTP resources. Go REST!

Java.net App Hosting

Monday, January 30th, 2006

Hong Zhang’s Blog mentions that java.net is now hosting J2EE applications for free through LocalWeb. Here’s the quote that scares me:

> The actual deployment of an application is done by a small team of engineers from Sun.

Yikes. Even with all the deployment descriptors, it takes “a small team” to host and deploy a single J2EE application. There’s something wrong with that.

I’ve always said that deploying Java applications is Java’s Achilles Heel. The LAMP community mocks Java not necessarily because the language is more verbose, but because it’s so dang hard to deploy the applications. Compare with PHP, which is as easy as editing the file and then hitting reload in your browser. That’s nearly impossible with a full J2EE application. So many libraries have memory leaks that play havoc with classloaders that a full application server restart is required.

In any case, this is one of the main reasons I’ve been moving to Rails. I can deploy an application in no time (especially when using SwitchTower).

Rails and Allowing ID Editing from Forms

Sunday, December 4th, 2005

Need to allow yours users to directly edit the ID of your model object?

By default, Rails will strip out the ID field from the hash of request parameters. This is a security measure, prohibitings the web interface from altering an object’s ID.

Generally, this is a good practice. However, some legacy schemas have IDs that are not simply numbers. In those cases, the ID is created and managed by the user.

To tell Rails to allow the ID to be edited, you can use attr_protected and attr_allowed.

More information is available at http://wiki.rubyonrails.com/rails/pages/HowToEnsureValidAttributesInFormData

Rails, Legacy Schemas, ActiveRecord, and has_and_belongs_to_many

Friday, December 2nd, 2005

There turns out to be an issue with legacy schemas and ActiveRecord, the ORM layer of Rails when using has_and_belongs_to_many.

Note: This issue might be Oracle specific.

If the join table itself has an id column, and it has the same name as the id column from the association table, then it will override the id from the association table.

This means that instances from the association table will have the wrong id.

The solution looks to be the new :finder_sql parameter of the has_and_belongs_to_many relationship. With :finder_sql, you can override the generated SQL.

How to use :finder_sql? Try:

has_and_belongs_to_many :ksaas, :join_table => "tbl_ksaa_objective",
  :foreign_key => "objective_cid", :association_foreign_key => "ksaa_cid",
  :finder_sql => "SELECT TBL_KSAA.cid, TBL_KSAA.ksaa_type, TBL_KSAA.ksaa_item, " +
    "TBL_KSAA.KSAA_TYPEID, TBL_KSAA.SAMPLE_BEHAVIOR " +
    "FROM TBL_KSAA LEFT JOIN TBL_KSAA_OBJECTIVE ON " +
    "TBL_KSAA.cid = TBL_KSAA_OBJECTIVE.ksaa_cid WHERE " +
    '(TBL_KSAA_OBJECTIVE.objective_cid = #{id} )'

Keep those single quotes in order to lazily interpret the id in the query.

Note that Hibernate does not have this problem, as it aliases all column names, even when not using joins. Therefore it doesn’t have the column naming conflicts.

Named Routes in Rails

Thursday, December 1st, 2005

From Ronny Haryanto via the Rails Mailing List:

Interested in URLs that look something like this?

http://www.example.com/project/submarine

You can tell Rails to parse that URL in order to call the appropriate method on the controller.

 You might be interested in Named Routes.
http://wiki.rubyonrails.com/rails/pages/NamedRoutes

In config/routes.rb:

   map.project ‘project/:projtitle’ :controller => ‘project’,
                                    :action => ’show’

In app/controllers/project_controller.rb:

   def show
     @project = Project.find(:first,
                             :conditions => [’title = ?’,params[:projtitle]])
     …
   end

Or something like that. Assuming your project titles are unique. Then
you can also use project_url (since you have map.project in routes.rb)
for urls in your views, e.g.

   <% for project in @projects %>
   <%= link_to project.title,
               project_url(:projtitle => project.title) %>
    …
   <% end %>

Named Routes is a really cool feature, and one of the best of Rails,
I’d say.

Thanks Ronny!

Using Columns with Reserved Names in Rails

Thursday, December 1st, 2005

From Rails Mailing List : How to access column with a reserved name?

def new_column
  read_attribute 'old_column'
end

def new_column=(value)
  write_attribute 'old_column', value
end

Testing with Rails

Tuesday, November 29th, 2005

A hint with writing tests with Ruby on Rails.  If you are using fixtures, the name of the fixture is the name of the database table, not the name of the model under test.

For instance, this code:

class TaskTest < Test::Unit::TestCase
  fixtures :tbl_task

The symbol :tbl_task is the name of the database table.

Adjust Log Level in Ruby on Rails

Sunday, November 27th, 2005

To adjust your log level for a Rails project, you have two options.

  1. Edit config/environment.rb and set a global log level. This will override all environments (development, production, etc).
  2. Edit config/environments/production.rb, for instance to change production’s log level. This is recommended. Simply add the line config.log_level = :warn to set WARN level. This will greatly decrease the amount of logging performed.