Archive for the ‘ SQL ’ Category

Jquery on Rails3 auto select

Hello Folks,

In earlier post of mine, we have seen, how to implement “Auto Select”, with the help of Prototype and Rails.

Now, that Rails-3 has been released, lot of changes are already made. In-built support for Prototype has been removed. Still if you wish to use prototype, all you need is to install its gem.

Here, I am covering the same example:

- Select ‘Country’ from the first drop down.
- Second drop down will populate the list of ‘States’. If you select any state:
- Third drop down will populate the list of ‘Countries’.

All I am using here is Rails-3, Rails-3 UJS, and Jquery.

I would also like to thank Ryan Bates, for the railscasts he provided to implement this using unobtrusive javascript.

Please download the application with the link provided and let me know your feedback.

System Requirements/Features Covered in this application.
- Ruby 1.8.7
- Rails 3.0.0 or higher
- Country -> State -> City Drop down
- Ajax Search

Steps to run the application:
- Create a schema. Change the database.yml accordingly.
- Run rake db:migrate
- Start the server.
- Please do read the README file.

jquery-autoselect

Regards

Joins in SQL

Sometimes, its very hard to keep Joins concept in mind.. Being a beginner, experienced SQL programmer, you should know the concept of JOINS.

In this article, I will show you the difference between:
1. JOINS and INCLUDE
2. How to you use JOINS in rails
3. JOINS over INCLUDE
4. JOINS and INCLUDE in RAILS-3

Let’s cover them in detail:
What is Join ?
The SQL JOIN clause is used whenever we have to select data from 2 or more tables.

To be able to use SQL JOIN clause to extract data from 2 (or more) tables, we need a relationship between certain columns in these tables.

Joins Type
1. OUTER JOIN
1.a. LEFT OUTER JOIN
1.b. RIGHT OUTER JOIN
2. Inner Join

Let’s take an example here:

The INNER JOIN will select all rows from both tables as long as there is a match between the columns we are matching on. In case we have a customer in the Customers table, which still hasn’t made any orders (there are no entries for this customer in the Sales table), this customer will not be listed in the result of our SQL query above.

The second type of SQL JOIN is called SQL OUTER JOIN and it has 2 sub-types called LEFT OUTER JOIN and RIGHT OUTER JOIN.

The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER keyword in most databases), selects all the rows from the first table listed after the FROM clause, no matter if they have matches in the second table.

So, far you must be cleared with what JOIN does!! Now, we will see what JOINS and INCLUDE gives us….

Consider these two statements:

User.find(:first, :include => :user_profiles)
User.find(:first, :joins => :albums)