Pages

Wednesday, March 9, 2011

Rails model relationship

       
                     Mr.Thyagarajan(sir) Took the class

Hi today i learning rails model relationship concept:

has_many,belongs_to: [habtm]
has_and_belongs_to_many:
has_many :through .

1.has_many,belongs_to:
             in this example is  more than employee working under the company
   
class Clinic < ActiveRecord::Base
has_many :employee
end
2.has_and_belongs_to_many:
               doctors have many patient , and patient have many doctors


class doctor < ActiveRecord::Base 
has_and_belongs_to_many :patient
end
         
 3.has_many :through :         
  This explanation is [has_many :through] concept

 im creating rails model relationship in mobile marketing(project)
  [MSR,clinic and doctor through (tour)program]

~$ rails new mmfa -d mysql
    first we are creating a rails

~$ cd mmfa/
   entering a project directory

~/mmfa$ rails generate model Clinic name:string address:string
    generate a rails in clinic

~/mmfa$ rails generate model Doctors name:string phone:string mail:string
  generate a rails in doctors

~/mmfa$ gedit config/database.yml
   edit the database and enter the password

~/mmfa$ rake db:create
~/mmfa$ rake db:migrate


~/mmfa$ rails generate model Tour clinic_id:integer doctor_id:integer
   tour is a bridge b/w clinic and doctor

~/mmfa$ gedit app/models/clinic.rb  app/models/doctor.rb  app/models/tour.rb
    edit three models

1.clinic.rb

class Clinic < ActiveRecord::Base
has_many :tours
has_many :doctors, :through => :tours 
end

2.doctors.rb

 class Doctor < ActiveRecord::Base
has_many :tours
has_many :clinics, :through => :tours
end

3.tour.rb

class Tour < ActiveRecord::Base
belongs_to :clinic
belongs_to :doctor
end

~/mmfa$ rake db:migrate 
next we are open a new tab entering a project directory

~/mmfa$ rails console
   entering a irb
  
 
 the example is given below
  example:

p=Clinic.new
=> #<Clinic id: nil, name: nil, address: nil, created_at: nil, updated_at: nil>
irb(main):003:0> p.name="bava"
=> "bava"
irb(main):004:0> p.save
=> true
irb(main):005:0> p=Clinic.new
=> #<Clinic id: nil, name: nil, address: nil, created_at: nil, updated_at: nil>
irb(main):006:0> p.name="kovai"
=> "kovai"
irb(main):004:0> p.save
=> true
 
entering a doctors name:
the example is given below
example:

p=Doctor.new
=> #<Doctor id: nil, name: nil, phone: nil, created_at: nil, updated_at: nil>
irb(main):022:0> p.name="mani"
=> "mani"
irb(main):023:0> p.save
=> true

irb(main):024:0> p=Doctor.new
=> #<Doctor id: nil, name: nil, phone: nil, created_at: nil, updated_at: nil>
irb(main):025:0> p.name="vadi"
=> "vadi"
irb(main):026:0> p.save
=> true


irb(main):036:0> p=Tour.new

=> #<Tour id: nil, clinic_id: nil, doctor_id: nil, created_at: nil, updated_at: nil>
irb(main):037:0> p.clinic = Clinic.find(3)
=> #<Clinic id: 3, name: "voc", address: nil, created_at: "2011-03-09 17:44:51", updated_at: "2011-03-09 17:44:51">
irb(main):038:0> p.doctor = Doctor.find(3)
=> #<Doctor id: 3, name: "kalai", phone: nil, created_at: "2011-03-09 17:52:01", updated_at: "2011-03-09 17:52:01">
irb(main):039:0> p.save
=> true
p=Tour.all

l> select* from tours;
+----+-----------+-----------+---------------------+---------------------+
| id | clinic_id | doctor_id | created_at          | updated_at          |
+----+-----------+-----------+---------------------+---------------------+
|  1 |         3 |         3 | 2011-03-09 17:54:22 | 2011-03-09 17:54:22 |
|  2 |         2 |         4 | 2011-03-09 18:06:14 | 2011-03-09 18:06:14 |
|  3 |         2 |         4 | 2011-03-09 18:07:16 | 2011-03-09 18:08:19 |
+----+-----------+-----------+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql> select* from clinics;
+----+--------+---------+---------------------+---------------------+
| id | name   | address | created_at          | updated_at          |
+----+--------+---------+---------------------+---------------------+
|  1 | bava   | NULL    | 2011-03-09 17:44:02 | 2011-03-09 17:44:02 |
|  2 | gandhi | NULL    | 2011-03-09 17:44:34 | 2011-03-09 17:44:34 |
|  3 | voc    | NULL    | 2011-03-09 17:44:51 | 2011-03-09 17:44:51 |
|  4 | ramana | NULL    | 2011-03-09 17:45:14 | 2011-03-09 17:45:14 |
|  5 | kovai  | NULL    | 2011-03-09 17:45:40 | 2011-03-09 17:45:40 |
+----+--------+---------+---------------------+---------------------+
5 rows in set (0.00 sec)

mysql> select* from doctors;
+----+---------+-------+---------------------+---------------------+
| id | name    | phone | created_at          | updated_at          |
+----+---------+-------+---------------------+---------------------+
|  1 | mani    | NULL  | 2011-03-09 17:51:09 | 2011-03-09 17:51:09 |
|  2 | vadi    | NULL  | 2011-03-09 17:51:37 | 2011-03-09 17:51:37 |
|  3 | kalai   | NULL  | 2011-03-09 17:52:01 | 2011-03-09 17:52:01 |
|  4 | revathi | NULL  | 2011-03-09 17:52:29 | 2011-03-09 17:52:29 |
|  5 | devi    | NULL  | 2011-03-09 17:52:51 | 2011-03-09 17:52:51 |
+----+---------+-------+---------------------+---------------------+
5 rows in set (0.00 sec)




No comments:

Post a Comment