

Working With Database

After creating database, we have to generate model. When we run ruby script/generate scaffold scaffold_name, it automatically generates controller, model, views, tests etc. To create table we have to run migration files. Generally these migrations files will be created when we generate a model. For active record handling through model, we have to follow some conventions.
(a) The table name is based on the model name.
(b) The table name will always be plural
(c) The table name will be in a lower case.
To create model, we have to pass the follwing commands :
i) ruby script/generate scaffold scaffold_name
ii) ruby script/generate model model_name
If we run these commands in console, the model and migration file will be created. After writing the migration for a particular migration file, we have to run the following command in console :
rake db:migrate
It will create the table with described field in the migration table.
Suppose we have created a model named as person and it has automatically created the migration file. Now we have to add the field declaration in the migration file. The example is as follows :
ruby script/generate model person
The model will be
class Person < ActiveRecord::Base
end
The migration file will be :
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :name
t.string :name
end
end
def self.up
drop_table :people
end
end
To create table, we have to run the migration command rake db:migrate . So it will create a table called people.
Save the record:
person1 = Person.new :name => Mike, :age => 30
person2 = Person.new :name => David, :age => 45
person1.save
person2.save
Two data will be saved in the table called people. The above Rails syntax is equivalent to the following mysql syntax :
insert into people ('name' , 'age') values ('Mike' , 30)
insert into people ('name' , 'age') values ('David', 45)
Read the record:
persons = Person.find :all
This will fetch all the data from the database. Its equivalent sql syntax will be given bellow :
Select * from people
person = Person.find 1
This will fetch only one record which has the id 1. Its equivalent sql syntax will be given bellow. Select * from people where (id = 1)
Read record with conditions:
In active record :conditions option is converted with where clause to retrieve the data from the database.
Persons = Person.find :all, :conditions => [ name =?, Mike]
person = Person.find :all, :conditions => name = 'Mike'
person = Person.find :all, :conditions => {:name => 'Mike'}
It will fetch data from the database where person name will be Mike. The equivalent SQL syntax is given bellow :
Select * from people where name = Mike
Read record with order:
To retrieve data from the database in a order(ascending and descending)
person = Person.find :all, :order => age DESC
person = Person.find:all, :order => age ASC
the equivalent sql syntax will be given bellow
select * from people order by age DESC
select * from people order by age ASC
Read record with group:
To retrieve data from database in group of a particular field, we can use the following Rails syntax :
person = Person.find:all, :group => age
The equivalent SQL syntax for the given rails syntax is given below :
Select * from people group by age
Dynamically find the record:
To find record dynamically we can use to rails method find_by and find_all_by in rails .The examples are given bellow
person = Person.find_by_name(MIke)
persons = Person.find_all_by_age(30)
Updates record:
For updation record attributes rails has two syntax given below.
person.update_attribute :name, "Rails"
person.update_attributes :name => "Rails"
here record will update with value Rails
Delete record:
For deletion of the row from the database, we can use destroy method. Here destroy is an object by which we can delete a record from the database.
person = Person.find 1
person.destroy
