Archive for the 'Rails' Category

Active Scaffold for Rails: Many to Many

Monday, May 21st, 2007

Yesterday I downloaded Active Scaffold and played around with it. I have to say its pretty nice and really easy to use.

  1. script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold
  2. create three tables items, baskets, and baskets_items (see SQL below)
  3. create a simple layout with two includes

    < %= javascript_include_tag :defaults %>
    < %= active_scaffold_includes %>
  4. create two models examples below SQL, Item and Basket and add has_and_belongs_to_many :items to Basket
  5. create two controllers one for Item and one for Basket and make sure each has the keyword active_scaffold
  6. navigate to items or baskets and enjoy!
SQL for tables


DROP TABLE IF EXISTS baskets;

CREATE TABLE baskets (
id INT(11) NOT NULL AUTO_INCREMENT,
basket_name VARCHAR(40) NOT NULL COMMENT 'name of the basket, should match event',
PRIMARY KEY (id)
)
ENGINE=INNODB;

DROP TABLE IF EXISTS items;

CREATE TABLE items (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL COMMENT 'name of the item',
qualities VARCHAR(40) NOT NULL COMMENT 'comma seperated list of qualities we may infer about the person consuming the item',
PRIMARY KEY (id)
)
ENGINE=INNODB;

DROP TABLE IF EXISTS baskets_items;

CREATE TABLE baskets_items (
basket_id INT(11) NOT NULL,
item_id INT(11) NOT NULL,
PRIMARY KEY (basket_id,item_id)
)
ENGINE=INNODB;

Many-to-Many models


class Basket < ActiveRecord::Base
has_and_belongs_to_many :items
end

class Item < ActiveRecord::Base
end

Controllers


class BasketsController < ApplicationController

layout "active_scaffold"
active_scaffold :basket

end

class ItemsController < ApplicationController

layout "active_scaffold"
active_scaffold :items

end

5 Great Things About Rails MVC layer

Thursday, April 26th, 2007

Everyone can learn something from looking at the Rails MVC framework. Here are some highlights

1) The model is feature rich, making it easy to data manipulation in the model. For example code is often put in the controller to determine if an object is an update or an insert. This code should live in the model; it shouldn’t pollute the controller
2) There are helpers which are only accessible in the view. So little if then statements, popup windows, and javascript generation may be tucked away with out the temptation to manipulate them before the view is rendered. Today there is too much side effect programming where a controller will generate view code.
3) RJS templates provide a mechanism for injecting javascript, while keeping it separate from the view and controller. This rocks, the javascript can live on its own manipulating the page after its rendered and it can read session data.
4) Patterns for manipulating hashes make parameter logic easy. Rails has patterns for removing empty elements from hashes and combining hashes together, then digesting the resulting hash. This gets rid of all the if/then logic to check if a parameter has a value
5) Lots of helpers. Link helpers, URL helpers, image helpers, javascript lib helpers. This ensures that all paths get a little wrapper tag. This is a life saver when developing multi-channel or multi-partner sites

Rails Routing by David Black

Friday, April 20th, 2007

I round a nice rails
routing roundup by David Black. The slides don’t have much in the way of text which is good ’cause they are easy to scan. Thats also bad if you started reading expecting an introduction.

For those of you who don’t know Rails take a peak and compare it to something you do know like Apache mod_rewrirte

10 reasons not to use rails

Wednesday, April 18th, 2007

Well I posted this before, but it disappeared . Here are 10 reasons you may not want to use rails

1) The learning curve is huge. Just the other day I wrote my own version of cycle I also learned how to inject javascript from a controller.
2) SDLC is broken for teams. Yes Rails is great for a team of one, but version control and deployment is monolithic. For example if two developers independently work on two different controllers with two different views there is no way to version and deploy these components separately.
3) Yet another deployment system. Rails has gems and plugins. The rest of the world uses rpms, debs, wars, and whatever windows uses.
4) Ruby is green threaded. One server is slow you’ll need many-many servers to handle the load. Good thing virtulization is taking off.
5) ActiveRecord breaks down when you want to pivot rows into columns. This use case comes up with catalogs with many topics, each topic containing its own fields. Luckily there is RBatis.
6) The code base is unstable. The main code line and the gems are constantly evolving with little consideration for backwards compatibility. Combine this with poor deployment managment
7) Multi-lingual support is broken. Multi-byte characters aren’t supported, so stick to one language!
8) Rails web containers are unstable. There are many choices for a web container webbrick, mongrel, and lighthttp to name a few. None of them work as well as Apache. The servers get wedged. Apache with Fast CGI doens’t seem to work and those who start using it move on to something else.
9) The code is obtuse and you’ll need to read it. If you want to understands how to do things you’ll need to read the code.
10) The conceptual documentation is missing. Its a framework, yet the concepts go unanswered. For example, what exactly are restfully resources for and why isn’t there a client library?

Ruby Highlighting in Emacs

Monday, October 30th, 2006

If you want to turn on syntax highlighting for Ruby in emacs, first download ruby-elisp, then add the following to your .emacs file.


(autoload 'ruby-mode "ruby-mode" "Load ruby-mode")
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
;; uncomment the next line if you want syntax highlighting
(add-hook 'ruby-mode-hook 'turn-on-font-lock)

I like a darker color, with the possible to escape if the colors don’t work out


(autoload 'ruby-mode "ruby-mode" "Load ruby-mode")
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
;; uncomment the next line if you want syntax highlighting
(add-hook 'ruby-mode-hook '(lambda ()
(set-background-color "slate grey")
(set-foreground-color "almond")
turn-on-font-lock))

(add-hook 'text-mode-hook '(lambda ()
(set-background-color "white")
(set-foreground-color "black")))

when ruby-mode is loaded the background is slate grey, to get a white back ground just start using text-mode

Bruce Tate endorses Rails

Wednesday, May 17th, 2006

Bruce Tate, author of books like Better, Faster, Lighter Java and the owner of a consulting company specializing in Java, has come out in support of Ruby on Rails. I liked his point about the feedback look, the other points I’ve heard before.

So in my opinion it is only a matter of time before some small, yet compelling website pops up using Rails. Heck the kids are all over Rails. Once that happens medium sized companies will be trying to use Rails, or larger companies will put some bits up on Rails.

Even Microsoft has taken an interest in Ruby. Which is pretty amazing to me, ’cause how many languages do you need? After all Rails is made great because its a good framework, not because its a good language.

Ruby on Rails vs Your Java Stack

Monday, May 15th, 2006

I’m fascinated by the discussions around Rail’s feasibility as a language. First off, the possible implementations are wide open, as if any platform should work everywhere all the time.

Second, its amazing how small the number professional Ruby programmers are to the number of Java programmers. Take a look at this O’Reilly chart. The number of Ruby book purchases are super small compared to Java.

This tells me there isn’t a coming wave of Rails in the enterprise, and that the Rails community is small, but vocal.

Don’t use book sale numbers to eschew Rails, just keep in mind the net is amplifying the discussion around Rails. In fact, I would go further to say the net is changing the way we use and evaluate software. Rails will probably be adopted faster than any other language in the history of computer science, due to better distribution and more information sharing.