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.

Tuesday, February 26

Ruby on Rails relevance to Startups - @ BarCamp

The barcamp5 hosted by Google Engineering Hyderabad :)
Many bloggers had already spoken their part about the proceedings through live blogging.
In brief, there was bit chaos on arranging things, running the talks/session parallely, and finally it was more like session with less user participation (devitation from what usual BarCamp famous for!!!)

Sumanth Krishna @ Ruby on Rails Conferences/Communities


I wanted to present on my favorite Ruby on Rails and since BarCamp is the place where lot of young minds, entrepreneurs... assemble. So I related it (RoR) to Startups and on "Ruby on Rails relevance to Startup's". I started the session post lunch around 3PM. I did present my views on very well about the startups and when the slides related to Ruby on Rails started, I just carried away more in explaining the details of RoR.


Slides: "RoR relevance to Startups" @ Google sponsored BarCamp5 :)
Scribd
SlidShare


Please feel free to enhance/comment on the presentation.
I am planning for a Workshop on Ruby on Rails with support of Twincling society :-)

Wednesday, February 13

Relationships between tables - How ActiveRecord handles?

Keywords:
  • has_one,
  • belongs_to,
  • has_many,
  • has_and_belongs_to_many
  • polyphormic
  • one-to-one
  • one-to-many
  • many-to-many
It's known that any web application will contain bunch of tables and they are dependent on each other. With normalization as the key, we avoid redundancy and have more relationships with tables using keys. In database terms, the following are the relations allowed/known between tables -> one-to-one, one-to-many, many-to-many. Let us digg more, how ActiveRecord handles these.
ActiveRecord supports the above mentioned relations and in fact, comes with set of key words which are easy to use, understand and pretty clean.
Let me take few snapshots from David's book:
Note:
  1. invoices is the table name
  2. orders is the table name
In the above snaphot, the motive is to declare the relationship between the orders and invoices tables. It's clear that orders will have invoices, i.e., invoices belong to orders. And watch out we will be using same words. Now to have the relationship declared, we will go to their models (Remember: the application access the tables through model so it makes sense to declare relationships also in models).

Now I am into order model, orders having invoices. so the key word that we use here is has_one and then followed by the model name (Note: It is not table name, this is not plural).
has_one :invoice

Next will go to invoice model, since invoices belongs to orders we use the key word belongs_to and then followed by the model name.
belongs_to :order

Well there is not xml written for models, no xml written for relationships. But simple convention and defined keywords done the job for us.
And as you might have guessed the has_many comes into use when the orders table is having relationship with one more table (say, line_items). The below snapshot will help you to understand the syntax and it's usage.

Will look into other keywords and other relationship in the following posts...

Thursday, February 7

Hyderabad hosts BarCamp5


BarCamp what is it?
BarCamp is an international network of user generated conferences — open, participatory workshop-events, whose content is provided by participants — often focusing on early-stage web applications and related open source technologies...
and the u can read more from here...

As part of series of BarCamp, this series is held @ Hyderabad. Into details....

Theme: Social web and beyond

Technologies, demos and startups- betting on Web2.0, Social web, Semantics Web and beyond!
Entrepreneurship
Social Web and Beyond
Opensource and Emerging Technologies
Product/Prototype/Idea Demos



On February 16th, 2008. Saturday

Venue ( Map of the venue )
Block 1, DivyaSree Omega
Survey No. 13, Kondapur Village,
Hyderabad, A.P 500 032
India.

Register @ Barcamp and hope to meet you there!
@ Orkut
@ Facebook

Spread the word and get the most of it!

Monday, February 4

ActiveRecord - Pluralization convention

In the previous post, we had seen how simply we had connected our database to the application. Now let us look in to another convention wherein, you need not have a xml file to connect each table in the database to the classes (Note: class is the blue print from where we create objects) in our application.
The convention here followed is English way of singular and plural forms. The database is nothing but collection of records, data in specific structure. And a 'class' as per Object Oriented Paradigm, it is a constructor and you can have multiple objects of similar type from it!
So it absolutely makes sense to name our tables in plural form (collection of data) and a class in the singular form.
Rails just does that! we name the tables of database as plurals and the respective classes as singular form of the same.

The above snapshot is taken from David's book on rails. As it shown here, we maintain the singular form on to the class names and plural on the table names.
Rails literally understands the singular and plural form of the words you use.

Order orders
Person people

You take a note of the way we write the names:
  • the table names in plural form all lower case letters.
  • the class name in singular form with camel case/capitalized word
Of course, I am sure you have few questions running into your mind:
  1. What if the word does not have proper plural form?
  2. What if I do not want to follow this?
The answers are:
  1. As you can see in the above snapshot, the 'Person' as the class name and 'people' as the table name. And if you do not have proper/matching form for it, or you did not maintain the convention mentioned - you can use a keyword called set_table_name, in the model class where ever you are defining the construct.
  2. You can disable this feature globally with a variable under 'config/environment.rb' of the application. ActiveRecord::Base.pluralize_table_names = false
Well, this is just a convention that help avoid writing/maintaining lot of xml files and connection problems. Let me also tell you that it is not mandatory to have this maintained strictly.
I guessed only the above questions, if you have more questions touch base with me and get them clarified.

Tuesday, January 29

ActiveRecord Databases - II

In the previous post, I just shown how do you tell your application the whereabouts of the database(s) that's in use. Now we are left out with task to connect each tables. Let me remind you again, this would need configuring/customizing lots of xml files in java based applications.
You may ask:
Come on, How do you achieve it without the xml files?
Well the magic here is usage of, pluralisation of tables and respective singular form for it's class names.

Can you show me how to do it?
hmmm... of course.
In last post we modified database.yml of our "dummy" project.

adapter: mysql
database: dummy
username: root
password:
host: localhost

Just recollect, this shows our "dummy" project is referencing to database by name "dummy". I am creating the database related tables, objects (rows), columns (properties) using a tool called phpMyAdmin.

Assuming that this "dummy" database contains a table with user information. So our convention would suggest to have table name in plural form. Let's call it as "users". Now we need to have a class created with singular form of the "users" -> "user", and respective logic relating to the table would go into/under this class.
class User <>

end

Let us diagnose a bit the above code. Recollect the convention we talked above, and the ORM concept. This means that the applications having class called "User" which would connect/contact to database table by name "users" (the database info is set up in database.yml).

We will slowly getting into the code and we keep developing or adding more functionalities to our "dummy" project.

Do I need to use only mysql as the database?
Not exactly and need not be. The following table would give the info related to configure each database and the respective parameters:


Well more action and fun is on the way :)