Feeds:
Posts
Comments

Archive for January 11th, 2011

Greetings !

Today our mentor Mr. Thyagarajan Shanmugam continued the session on Rails.

During the start of the class, we had a quick recap of previous classes i.e., steps to insert values into MySQL via PHP script Vs MySQL via Rails script

I already shared my experiences on these learnings in my blog posts

https://sraji.wordpress.com/2011/01/06/how-to-insert-values-into-mysql-via-php-script/https://sraji.wordpress.com/2011/01/07/how-to-insert-values-into-mysql-via-ruby-on-rails/

From his class i learnt

a. How to create two scaffolds for a single rails project ?

b. How to specify a relationship between the two scaffolds in view level as well as database level ?

c. How to add a field/column name to specify the relationship between two tables ?

Creating the rails project and first scaffold


rails new -d mysql blog

cd blog

gedit config/database.yml (to enter the password of mysql)

rails generate scaffold Contact name:string address:string

rake db:create

rake db:migrate

rails server

Open the browser @ http://0.0.0.0:3000/contacts

Add data to the fields listed in the web page

Verify the data insertion by migrating into mysql prompt

Generating second scaffold


rails generate scaffold Typeofcontact name:string

rake db:migrate

Open the browser @ http://0.0.0.0:3000/typeofcontacts

Add data as client,vendor,supplier in the name field

Also, verify the data insertion in mysql db.

Now  to specify the relationship in database level edit app/models/contact.rb


class Contact < ActiveRecord::Base
belongs_to :typeofcontacts
end

Edit app/models/typeofcontact.rb


class Typeofcontact < ActiveRecord::Base
 has_many:contacts
end

By using the rails command, we can specify the relationship between two scaffolds in view level


rails generate migration AddFieldToContact typeofcontact_id:integer

rake db:migrate

The above command will add a field name ‘ typeofcontact ‘ to contacts table under blog_development database

So, from today’s class i clearly understood the concepts i mentioned above.

My teammate Ramachandran  helped me to get understand fully with the above concepts

But our mentor taught some stuffs beyond these concepts which i dont understand clearly

Hope to get clear by tomorrow

By 4.05, we had a scrum meet and i told as i have learnt on how to add a field to a database table

Advertisements

Read Full Post »