Satya's blog - 2009/06/

Jun 01 2009 22:47 Ruby on Rails and option_groups

Ruby on Rails rocks my socks again. I had a need for a drop-down with options from many "question sets". I did this in the controller:

@disclosure_questions=QuestionSet.find(:all,
        :conditions => ['is_screener=?', false], 
        :include => [:questions])

So QuestionSet was my outer collection, and each one has many questions, hence the include.

Then in my view I did this:
<select name="disclosure_id">
< option_groups_from_collection_for_select(
        @disclosure_questions, 'questions', 'title', 'id', 'number')
%>
<select>

Which says, make option groups from the disclosure_questions, which is a collection of QuestionSet. The sub-groups are composed of questions, which is a method called on each QuestionSet (by virtue of has_many). title is an attribute of QuestionSet, and id and number are attribute of each question.

More help at the Rails API site

Tag: rails

Jun 05 2009 14:47 Shibboleth 2 on Ubuntu 9.04
I recently tried to install Shibboleth as a Service Provider on Ubuntu 9.04. Shibboleth 1.3 is End-of-lifed June 2010, so the shibboleth-users mailing list advised me -- strongly -- to use Shibboleth 2. Well, Ubuntu 9.04 doesn't have the packages for it. Debian Lenny does. So here's how you get a Shibboleth 2 SP (Service Provider) on Ubunt 9.04:

Remove libapache2-mod-shib and auto-loaded packages, if you had them installed. Then, install some of the packages required by the shib2 module:

aptitude install libsaml2 unixodbc opensaml2-schemas xmltooling-schemas

and dependencies. The aptitude command will take care of the dependencies.

Download and install the following. These are the shib2 package from Debian lenny (current stable).

http://ftp.us.debian.org/debian/pool/main/s/shibboleth-sp2/shibboleth-sp2-schemas_2.1.dfsg1-2_all.deb
http://ftp.us.debian.org/debian/pool/main/s/shibboleth-sp2/libshibsp1_2.0.dfsg1-4_i386.deb
http://ftp.us.debian.org/debian/pool/main/s/shibboleth-sp2/libapache2-mod-shib2_2.0.dfsg1-4_i386.deb

Or go find them yourself, since the particular versions listed are sure to be obsolete after this article is posted. You want shibboleth-sp2-schemas libshibsp1 libapache2-mod-shib2, in that order. Download the .deb files and run

dpkg -i shibboleth-sp2-schemas*.deb libshibsp1*.deb libapache2-mod-shib2*.deb

Last updated: Jun 05 2009 18:42

Tag: geeky shibboleth

Jun 13 2009 00:20 ActiveRecord::find() in Ruby on Rails

When I first started with Ruby on Rails, it took me some time to figure this out. ActiveRecord's find() method will return two different kinds of objects when you call it with :first versus :all.

find(:first) returns a single object. find(:all) returns an array. If no records are found (perhaps because conditions didn't match), find(:first) returns nil, while find(:all) returns the empty array. find(:all, :limit => 1) returns an array, perhaps with a single object in it. find(:first, :limit => 1) or even :limit => 2, returns a single object (or nil).

The result of find(:first) can be checked with the nil? method, and the result of find(:all) can be checked with the nil? as well as empty? method (but nil? should return false if the find() actually was executed).

In perl terms, find(:first) always returns a scalar, find(:all) always returns a list.

Last updated: Jun 13 2009 00:21

Tag: rails

Jun 13 2009 00:23 Ruby on Rails instance variables explained

Instance variables (@foo, for example) are a Ruby concept, not Rails. But it's a little confusing in the Rails' Model-View-Controller paradigm.

Consider that in pure Ruby, an instance variable is available to an object (which is *instantiated* from a class). The instance variable can be used and changed by any methods of the object. This is unlike non-@ variables, which are limited to the scope where they're defined:

def ex1
   foo=1 # defined within the ex1 method
end

def ex2
    if @foo==1
        bar=1
    end
    bar # nil, because it's out of scope
end

def ex3
    @foo=1 # remains defined after ex3 returns
end

Now here's my point: in Rails, instance variables defined in a controller method are available to the view, but not to any models called from that method. Why?

Because a Rails view is NOT a separate class. It's a template and it's part of the current controller object. A model is a separate class.

That's all. It's a simple point, but it can be confusing at first to those not familiar with Ruby or Object Oriented Programming.

Tag: rails