Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

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

Monday, January 28

ActiveRecord Databases - I

One of the main features that rails do support on is Convention over Configuration. Well repeat again and again Convention over Configuration. Let it get into the minds!
Well it is spread across the rails framework with which will be developing an application. I will be reminding whenever it's put in use.
In previous post, I did stress on how simple it would be in configuring the databases and tables with your application. In Java based applications, this would be done using lot of xml files, which is of course, time taking, lot of repetitive work and error prone... (at least that's what my friend's used to say!).

Well in rails this is not the case a simple convention that you follow would ensure of configuring the tables of databases.
"You name the tables in database as plurals and create classes with singular form of the table names."
Configuring the Databases:
The choice of which database you use the configuration and respective adapter would depend on. When we created an application, there is a set of predefined folders created over a simple command! (I will touch upon this later). "config" is one such folder and under this we will now concentrate/repair on "database.yml".
Rails would automatically create 3 different environments:
  1. Development
  2. Test
  3. Production
Based on the environment we work on we need to edit the data accordingly. (More on environments will be covered sooner!)
This file contains the information relate to the adapter, database, user credentials, host, port...

This does make sense in a way, the plural form for tables will fit as it contains huge data and the respective class which we would be placed under app/models, with the extension ".rb" is named in singular form.


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

What it means?
Our application is connected to MySQL database by name dummy, user being and root and with an empty password. The host environment is "localhost".

To Be Continued...