Wednesday, March 19

ActiveRecord - Connecting to DB and tables

I thought compiling of all my previous posts and documenting would be of great help to all users including off line. I compiled all the posts where I discussed on
Connecting to databases
Pluralization convention
Relationships between tables
...
Download here

View on Scribd

this would help all to keep in touch before we go deeper on to more Active Record.
more fun awaiting :-)


Thursday, March 13

Firefox Profile Manager

I am fond of using the one of the key feature of Firefox browser i.e., Profile Manager. For developers especially, it means not to mess up our work.
I prefer to use each different profile for the different extensions/add ons that I am working on for Firefox browser and not to mess up these with my default profile which I mail use for browsing and mailing purposes.
For the benefit of first timers, profiles can be created (in Windows) from the run command window
I run teh command
  • firefox.exe -P
  • firefox.exe -profilemanager

With Minefield (Firefox 3.0*) around we need to start our development/testing even on Minefield apart from regular FF2.0*. Now the same old commands of creating profiles firefox would not work...
Since the minefield is intalled, and being latest the above commands would result in creating profiles in the Minefield but not into the Firefox. There is another command which many might have forgotten:

  • "C:\Program Files\Mozilla Firefox\firefox.exe" -profilemanager
  • "C:\Program Files\Minefield\firefox.exe" -profilemanager

Thanks to my friend Aruna, whose query had made me dig into Profile Manager of Mozillazine Knowledge base and resulted in writing this post.

ActiveRecord association has_many :through

We had seen how a has_and_belongs_to_many works with a join table. But apart from carrying the foreign keys the join table has nothing much to do over there. So to have more features added and retaining the goal of having many-to-many relationships we will discuss now another rails offering "has_many, :through".

The above snippet shows a simple example from Josh Susser's blog.
The scenario shows that there are two tables "books" and "contributors" and the a join table "contributions". This is entirely different from habtm, where we had the join table with combination of tables.
And syntactically, we say to each of tables (books, contributors) that they are related to each other through "contributions".
So the join table would have a simple belongs_to and the individual tables will be related with has_many:'table_name' and keyword :through=>'name_of_join_table'

That is with the ActiveRecord relationships. Just to recap
has_and_belongs_to_many
has_many, has_one, belongs_to

We will be slowly moving into other part of rails packages, ActionPack soon.

Sunday, March 9

ROR Workshop -1

Ruby on Rails workshop went on well. As stated in my previous post, I introduced the Ruby and Rails. Spent bit time around how the web applications work in general and the features that rails equipped with to address the each and every requirement.
Though I am not getting into more details in this post, soon I will be writing/discussing more about them. I thought I would thank one and all in making this workshop pretty informative and very good overall.
Members of Twincling Community
Saifi
Namita
Sumith

My Friends
Rajesh Batta
Hareesh Kumar

My Juniors
Anas
Hafeez
Haritha
Lokesh
Rasheed
Sravanthi

and the great audience. At the end of the day, I felt very very good when few of the attendees asked details about next session.

In fact, I would like to cover few other topics like, Testing in Rails, Exceptional Handling, Migrations & Generators and Plugin integrations ... feel free to comment :-)

Friday, March 7

RoR Workshop @ Hyderabad - Twincling

I am planning for a workshop on Ruby on Rails introductory level. The agenda of the workshop is

  • to introduce the Ruby on Rails
  • MVC Architecture
  • ActiveRecord
  • ActionController
  • ActionView
  • ActionMailer
  • Pagination
  • Ajax
  • Rails demo with project
For more details about the venue and organisation details, visit Twincling.
More about the workshop will be updated through the posts, keep watching :-)

Saturday, March 1

has_and_belongs_to_many : habtm in Rails

has_and_belongs_to_many, simply "habtm" of rails would address/handle the many to many relationships of the tables.

Let us take this scenario where we have two tables, "products" belong to "categories" and "categories" belongs to "products" in more than one ways. That is each product belongs to many categories and each category contains many products. In each of the model, category and product the relation would be shown using habtm (has_and_belongs_to_many).






Closely observing the above example, we can see that the products table and categories table contains habtm relation. There is an intermediate table "categories_products", which would carry the primary key of each table.

At the database level this is achieved using an intermediate join table. The convention followed here is the name of the join table is the concatenation of the two target table names but in alphabetical order. In our example, we joined the table categories to the table products, so Active Record will look for a join table named categories_products.

Note:

  1. The join table carry the foreign keys of each table.
  2. There will be no model for this intermediate table (categories_products)

Well that precisely explains how we implement many to many relationships in Rails. That is not all there is one more way of expressing the relationship between tables, "has_through" which we will touchbase later.