Wednesday, April 9

Build your own OpenSearch/Sherlock engine

It's been long since I blogged anything other than Ruby on Rails. I am trying my best to make RoR tutorials as simple as possible. Just little bit deviation from RoR but still on to Web based topics - "Browsers".









I thought of writing a post on creating/building an opensearch engine for your own site, but I just documented for better understanding and made it availble to download/view online.

The document covers the two approaches to create a search client. The code snippet contains the code with sphred as the example site.








View on Scribd:
Download :


Feedback/Suggestions welcome :-)

Thursday, April 3

ActiveRecord - CRUD operations - 2

We learn t how to create rows in the database. Moving next in CRUD, it is Reading data from db tables. Reading data involves/requires set of constraints/conditions. Something like, details of specific user, product pricing range, sorting order, matching criteria... and the list goes on. To go ahead with these operations ActiveRecord provides the following handy methods, parameter symbols to you:

find
find_by_xyz
find_by_sql

:first :all :conditions :include :order :limit :select :joins :offset :readonly :lock
etc...

Getting into details, previously we created few rows about user into users table. Now we use this find to read/retrieve the data. Little about "find", it is counterpart for "select * from users" in SQL. The syntax is very plain and simple to pick up.
Say, now I want to read/retrieve the data of all users.

User.find(:all)
so the find method connects to users table (through User class of model) and through parameter "all" it fetches info of all the users a simple array. If we use parameter :first, then only first record would be fetched.
User.find(:first)
say now I want to introduce some constraints. :conditions is the parameter to be used.
Like, I want user with a specific name
User.find(:first,
:conditions => "name='sumanth'" )

but in case the name to be matched comes dynamically through params, then there would be slight change in the above syntax. Like this,
User.find(:first,
:conditions => "name=?,params[:name] ")

or

User.find(:first,
:conditions => "name= '#{
params[:name] }'")


if you want more than one parameter to be checked/matched in the conditions then we can separate each with 'and'.

User.find(:first,
:conditions => ["name=? and email=?",params[:name] ,params[:email]])
thus we can play around with more options/parameter that "find" accommodates. Don't forget to separate each options with "," as I had used it after :first and before :conditions options.

Wednesday, April 2

ActiveRecord - CRUD operations -1

Having seen how do we handle the relationships across db tables through rails now let us dig little deeper on how to perform CRUD operations.
Create
Read
Update
Delete
Well Rails ActiveRecord does help you out with lots of keywords and many more convenient, easily understandable, meaningful commands. In nut shell, you can carryout all these sql queries without using sql syntax at all...

Inserting data - We generally use SQL Insert statements to insert/create new rows of data and Rails offers a simple way of creating new rows in tables. One of doing it is by using "new" or by "create" method.
The syntax looks very simple, as below:

Based on previous posts we can coolly observe/interpret the above code like this:
  1. As per ActiveRecord pattern in rails, this says that User is the model connecting to users table in database.
  2. All the columns (eg:name here...) of the "users" table go in do ... end loop, 'u' is the iterator.
  3. Once you set the values of each column then end with save (u.save).
Why to save?
Because, new method would only creates an object and so to store into db we need to explicitly say it to save.
Is there any alternative one?
In many instances, people create objects, define the data and they tend to forget saving the data. Active Record offers another method, create, which would instantiates the model object as well as stores it into the database.


Using both new, create methods we can either insert multiple rows of data or even the form data can be passed as parameters.

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.

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 :-)