Kick-Off: First Rails 4 Application with HABTM Association

Hello Folks,

Its been a long time since I’ve not written a post. Now, I realize that it’s time to share some useful snippet with all of you.

Recently, I got a chance to get my hands cleaned with Rails 4. So much is happening around Rails community and with the rapid development of Rails 4, I was finding it bit difficult to keep my momentum going.

Well, leave all this apart. Now, by putting some extra efforts my system is ready with:

Ruby 2.1 & Rails 4.1

In order to start with my first Rails 4 Application, I took HABTM Association (weird, isn’t it?). It came randomly into my mind. However, I will dig into other associations as well with time, but to start with let’s take HABTM association with example.

I assume most of you are aware what changes Rails 4 has introduced. If you’re new to Rails 4 or would like to re-visit the changes, here’s the most useful link I’ve found. Just take a look.

Once you’re sure about changeset in Rails4, let’s draw an example:

Prerequisites:

  • Ruby 1.9.2+
  • Rails 4.1.0+
  • Traditional Database (MySQL/Postgres)

Key-Entities:

  • Person Model
  • Communtiy Model
  • A Join Table

Defining the Relationships & creating tables:

For rest of the code-flow (controllers, views) you can download the zip of the application from my GitHub page, run it on your local and see Rails4 HABTM in action.

Happy Coding 🙂

Advertisements

Installing mysql2 gem with Ruby 2.0

Hi Folks,

In this short tutorial of mine, I am going to write down the steps of how you can install mysql2 gem with Ruby2.0 on Windows machine.

Installing Mysql gem is always a big time pain for most of the developers who are working on Windows machine. But thanks to active folks over the internet for their comments, posts.

Prerequisites:

1. Ruby 2.0.0 pre-installed.
2. DevKit installed
3. gem install rails – command should install all the gems. Return should be success.

Steps to install mysql gem:
1. Go to: http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick, download will start automatically (don’t think at this moment that you’ve a 32 bit or 64 bit operating system.

2. Extract the folder to some place (in my case I am extracting it to C:\mysql-connector-c-noinstall-6.0.2

3. Run this command:

4. Output will return in below manner:

5. If you see this message, create a new Rails4 application, open your Gemfile and use:
gem 'mysql2', '0.3.13'

and run bundle install. Once bundle install runs successfully. You should try:
rake db:create and
rake db:migrate

to ensure that gem is installed correctly.

Hope this will help someone!

Happy Coding!! 🙂

Displaying Something from The Database

Guys in this tutorial I will explain you how to fetch values from the database and show those in a Table format.

Here I assumed one thing, that the user is logged in. and that user has the rights to delete a particular record from the table.

NOTE: This tutorial is very old. But will glimpse you how you can format your data in a structured manner.

So are you Guys Ready???? Then Let’s Start……..

Note: This will start the session of that particular user.


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:

<images comes 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)

<I am going to update this post soon.>