Rails and Database Tables
We shall now create a web interface that lets us maintain a student database - create new student records, edit existing student records, delete unwanted ones, and so on. We'll develop this application in small iterations. We'll start off by creating a new Rails application. This is where we'll be doing all our work. Next, we'll create a database to hold our information.
Open a command window and navigate to our folder 'work' where the path on my machine is c:/rails/work Switch to the work folder and use the rails command to create an application called student, as follows.
c:/rails/work>
Create the database student
In a command window type:
c:/rails/work/student>
You should now get a mysql prompt. Next at the mysql prompt, type as follows:
mysql>
It will respond with:
Next, on the mysql prompt, type:
Then type:
mysql>
Let's write the Data Definition Language (DDL) for creating the student table in MySQL. Your application student has a sub folder db where we shall store this file create.sql
Now use the mysql client to execute the DDL and create the table in our student database. Type:
c:/rails/work/student>
This would have created our
Rails Naming Convention
Rails assumes that:
Technorati Tags: Rails and Database Tables
Blogs linking to this article
Open a command window and navigate to our folder 'work' where the path on my machine is c:/rails/work Switch to the work folder and use the rails command to create an application called student, as follows.
c:/rails/work>
rails student
Create the database student
In a command window type:
c:/rails/work/student>
mysql -uroot
You should now get a mysql prompt. Next at the mysql prompt, type as follows:
mysql>
create database student;
It will respond with:
Query OK, 1 row affected (0.00 sec)
Next, on the mysql prompt, type:
mysql>grant all on student.* to 'root'@'localhost';
Then type:
mysql>
exit
Let's write the Data Definition Language (DDL) for creating the student table in MySQL. Your application student has a sub folder db where we shall store this file create.sql
drop table if exists participants;Rails assumes that every table it handles has as its primary key an integer column called
create table participants (
id int not null auto_increment,
name varchar(100) not null,
city text not null,
primary key (id)
);
id
Internally, Rails uses the value in this column to keep track of the data it has loaded from the database and to link between data in different tables.Now use the mysql client to execute the DDL and create the table in our student database. Type:
c:/rails/work/student>
mysql student <db/create.sql
This would have created our
participants
table.Rails Naming Convention
Rails assumes that:
- database table names, like variable names, have lowercase letters and underscores between the words.
- table names are always plural.
- files are named in lowercase with underscores.
- That the corresponding database table will be called line_items. That's the class name, converted to lowercase, with underscores between the words and pluralized.
- Rails would also know to look for the class definition in a file called line_item.rb (in the app/models directory).
- Rails assumes the class is called StoreController and that it's in a file named store_controller.rb in the app/controllers directory.
- It also assumes there's a helper module named StoreHelper in the file store_helper.rb located in the app/helpers directory.
- It will look for view templates for this controller in the app/views/store directory.
- It will by default take the output of these views and wrap them in the layout template contained in store.rhtml or store.rxml in the directory app/views/layouts.
First Post | Previous | Next
Technorati Tags: Rails and Database Tables
Blogs linking to this article
5 Comments:
Thanks Raj that helps.
Scot, I am not much of a databse guy. WHat I wrote in the blog, worked for me. Is the -uroot necessary?
in case i want to override the conventions then how to do that
Gaurav, you can override all of these conventions using the appropriate declarations in your Rails classes.
C:\rails>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18 to server version: 5.0.22-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database student;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on student.* to 'root'@'localhost';
Query OK, 0 rows affected (0.02 sec)
mysql> grant all on student.* to 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> connect student
Connection id: 19
Current database: student
mysql> show tables;
Empty set (0.00 sec)
mysql> drop table if exists participants;
create table participants (
id int not null auto_increment,
name varchar(100) not null,
city text not null,
primary key (id));
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.19 sec)
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| participants |
+-------------------+
1 row in set (0.00 sec)
mysql> desc participants;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| city | text | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)
Post a Comment
<< Home