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


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

No comments: