768x90 Getting Online Shouldn't be Tough- $7.49 .com Domains at Go Daddy

Active Record - Session 1


Ruby is not Unidentified Programing Object, but it is Universal Programing Object


As we know that rails is built by MVC design. Let we understand MVC (Model-View-Controller) instantly.



  • Model contains all information of operations or tasks that corresponding to process your database like CREATING, READING, DESTROYING, and UPDATING your database. Transaction Support and Store Procedure are created in Model too. But Transaction Object is in Controller.

  • View, it's all about display of your information or output of process. Your webdesign, information that want be displayed to realworld or user and output of data process are managed in View files.

  • Controller is brain of everything. Like the name, it controlls all process and also filter for all information of input or output. example before data displaying to realworld/ your user, it should pass controller first .

So where is Active Record in Ruby on Rails? it is in model. Active Record is Ruby Library that allows your Ruby programs to transmit data and commands to and from various data stores,whic are usually relational database. If you dont have Active Record, dont go away. Calm down and type this gem install activerecord to your ruby cmd. Let's implement it.


Create a Record


email = Email.new
email.username = "teapoci"
email.password = "myfav_tea"
email.customer_name ="Ariand Handsome"
email.customer_country = "Indonesia"
email.save

The generated SQL would be

INSERT INTO emails ('username', 'password', 'customer_name') VALUES ('teapoci', 'myfav_tea', 'Ariand Handsome')



Reading a Record

all_email = Email.find(:all)
first_email = Email.find(:first)
email_by_id = Email.find(10)

The generated SQL would be

SELECT * FROM emails;
SELECT * FROM emails LIMIT 1;
SELECT * FROM emails WHERE id = 10;


How to use :conditions

Email.find(:all, :conditions => ["customer_country = ?", "Indonesia"] )
Email.find(:all, :conditions => [:customer_country => "Indonesia" )
Email.find(:all, :conditions => "customer_country => 'Indonesia' "

The generated SQL would be

SELECT * FROM Emails WHERE customer_country = 'Indonesia'

-------------------------
---------------------------------------------------------------------------

Email.find(:all, :conditions => ["customer_name LIKE ? AND customer_country != ?", "%ar%", "Indonesia"] )

The generated SQL would be

SELECT * FROM emails WHERE
customer_country LIKE '%ar%' AND customer_country NOT IN ('Indonesia');


How to use :include

:include is like join table in SQL, it will look like nested hash of ruby in its script.



class Customer < ActiveRecord::Base

has_many :invoices

end



class Invoice < ActiveRecord::Base

belongs_to :payment

end



class Payment < ActiveRecord::Base

belongs_to :processor

end



class Processor < ActiveRecord::Base

end



Customer.find(:all. :include => { :invoices => {:payments => {:processor = > {} } } }, :conditions => ["processor.name != ?", "paypal"])





The generated SQL would be

SELECT * FROM customers
LEFT OUTER JOIN invoices ON customer.id = invoices.customer_id
LEFT OUTER JOIN payments ON invoices.payments_id = payments.id
LEFT OUTER JOIN processors ON payments.processor_id = processor.id
WHERE processors.name NOT IN ('paypal');


How to use :order

Costumer.find(:all, :order => "created_on DESC, last_name, first_name")

The generated SQL would be

SELECT * FROM customers ORDER BY created_on DESC, last_name, first_name


How to use :select

Costumer.find(:all, :select => "customers.last_name, 'family name' " )

The generated SQL would be

SELECT last_name AS 'family name' FROM customers;


How to use :group

Costumer.find(:all, :group => "state" )

The generated SQL would be

SELECT * FROM customers GROUP BY state;



How to use :joins

Costumer.find(:all, :joins => "LEFT JOIN invoices ON customers.id = invoices.customer" )

The generated SQL would be

SELECT * FROM customers LEFT JOIN invoices ON customers.id = invoices.customer_id


How to use :from

Costumer.find(:all, :group => "state" )

The generated SQL would be

SELECT * FROM customers GROUP BY state;


to be continued....


No Response to "Active Record - Session 1"

 
powered by Blogger