Archive for the ‘Ruby’ Category

Spring 2.0 Gets Scripting Support

Thursday, January 5th, 2006

It looks like Rob Harrop is now moving scripting into the main tree with support for Groovy, BSF and JRuby.

Spring, meet Ruby.

Real Lessons for Rails Deployment

Wednesday, January 4th, 2006

James Duncan Davidson has written up some Real Lessons for Rails Deployment. With the introduction of Switchtower, deploying with Rails is a piece of cake. Read on…

> It’s these lessons that I want to share with you on this Christmas Eve. I know that some of you will want me to just to say “Hey, here’s how you do it when you need xyz…”. And, I’ll get to those in later essays (this one is already too long at over 2500 words). But for now, the lessons. Later, we’ll get to the recipes.

Freezing your Rails when you deploy shared

Tuesday, January 3rd, 2006

Freezing your Rails when you deploy shared applications in order to protect your app from changes made by the hosting company.

> If you’re running a Ruby on Rails application on a shared host, it’s super-double-plus recommended to freeze your Rails. Freezing your Rails means putting the framework into vendor/rails instead of floating with whatever gems that are installed on the host. Because if you do so, you’ll automatically be upgraded when they are. Not a great thing for a production application to have forced upon itself.

> The great news is that this is silly simple. If you’re running 0.14.x or newer, you can simple do rake freeze_gems, and the current gems the system is used are unpacked into vendor/rails. Now the host can update as silly as it wants without affecting your application.

File Uploads with Rails

Monday, December 19th, 2005

Sebastian Kanthak has created a very handy file upload utility for Rails called FileColumn.

> This library makes handling of uploaded files in Ruby on Rails as easy as it should be. It helps you to not repeat yourself and write the same file handling code all over the place while providing you with nice features like keeping uploads during form redisplays, nice looking URLs for your uploaded files and easy integration with RMagick to resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database.

Rails Petstore

Monday, December 19th, 2005

The Rails Petstore is

> an implementation of Clinton Begin’s JPetstore that has been developed with the Rails web framework. The aim of this project is to develop a reference application that demonstrates the capabilities of the framework and the best practices that should be followed when developing an application.

This is for all you Java people out there who have seen the Petstore application in one form or another over the years. The Petstore is a common Java application implementation used to show how a framework is used “in the real world”. It’s been implemented many times, and comparing a Java Petstore to a Rails Petstore is very useful.

Ruby off the Rails

Sunday, December 18th, 2005

Ruby off the Rails is a look at Ruby from a Java developer’s point of view.

> Ruby on Rails is just one facet of what makes Ruby great, just like EJB is only part of the Java™ enterprise platform. Andrew Glover digs beneath the hype for a look at what Java developers can do with Ruby, all by itself.

On the Rails Again

Friday, December 16th, 2005

Danny’s Blog has some good perspectives from a Java guy who just went to JavaPolis, but who is also On the Rails Again. He did pick up on a general meme of EoD:

> A lot of presentations I saw, about Java EE 5 (GlassFish) but also for instance about Spring, stressed that everyone now focusses on EoD(Ease of Development) and ‘code by exception’, meaning: only having to code the exceptions to the default behaviour.

Having just come back from The Spring Experience myself, and having written Ruby on Rails applications for the two weeks prior, I can attest to what Danny experienced. Programming in Rails certainly opens up your eyes to what else is possible in the web programming world. Not all of it is good, mind you. You immediately miss the power of your Java IDE, for one thing.

I recommend that every Java web developer go code a quick application in Rails, even if they will never use Rails again. It’s so easy to get caught up in a language’s constraints, both physical and mental. By programming an application in something not Rails, you take a breath of fresh air. Rails is cutting edge because of what it doesn’t try to do. I’m certain that a Java developer can learn a thing or two from what Rails attempts.

Ruby to JMS Bindings

Tuesday, December 13th, 2005

You can now integrate Java and Ruby with JMS for Ruby. This implementation uses ActiveMQ as the JMS server. This rocks.

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.