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:


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

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.

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.
say now I want to introduce some constraints. :conditions is the parameter to be used.
Like, I want user with a specific name
: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,
:conditions => "name=?,params[:name] ")


: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'.

: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.
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 (
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.