Rails Models

14
How to create relationships between models June 17, 2010 [email protected] MeetUP @ Balabit

Transcript of Rails Models

Page 1: Rails Models

How to create relationshipsbetween models

June 17, [email protected]

MeetUP @ Balabit

Page 2: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Model View Controller

Model ViewController

http://domain/:controller/:action?p1=v1&p2=v2

HTML, XML, JSON, CSV, ...

router

Page 3: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Model & Database

id name

1 scb

2 syslog-ng

3 zorp

Table productsclass Product

Model layer(memory)

Mysql, Pgsql, Sqlite,Oracle, Mssql

(hard disk)

Product object

Page 4: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Creating a new row

product = Product.new

1.

product.name = “ssb”

2.

product.save! / product.save

3.

class Product

id name

1 scb

2 syslog-ng

3 zorp

4 ssb

...

Table products

validation

error

4.

Model(memory)

Database(disk)

Page 5: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Validating

class Product validates_presence_of :name validate :my_validator def my_validator

if name.length < 3 errors.add :name, “is too short” end

endend

Page 6: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Built-in validators

validates_uniqueness_of :username

validates_presence_of :username, :password, :email

validates_size_of :username, :password, :within => 5..15

validates_format_of :email,   :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

validates_format_of :username, :with => /^\w+$/i,   :message => "can only contain letters and numbers."

validates_confirmation_of :password

validates_inclusion_of :gender, :in => %w(male female)

validates_acceptance_of :eula

Page 7: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Fetch information

class Product

id name

1 scb

2 syslog-ng

3 zorp

4 ssb

...

Table productsproduct = find_by_name(“scb”)

product.id# 1

Page 8: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

FindProduct.find :all == Product.all

Product.find :first == Product.first

Product.find :last == Product.last

Product.all( :limit => 10, :offset => 3, :select => “name”, :order => “name desc” )

Product.find(1, 5, 10)

Product.all( :conditions => { :name => ["scb", "zorp"] }, :readonly => true )

Product.find_by_name(“scb”)

Product.find_by_id_and_name(1, “scb”)

Product.find_by_sql(“SELECT * FROM products”)

Product.exists?( :name => “scb” )

Product.all :conditions => [“id = ? or name = ?”, 5, “scb”]

Product.all :conditions => [“id = :id or name = :name”, {:id => 5, :name => “scb”}

Page 9: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Destroy

scb = Product.find_by_name “scb”scb.delete

Product.delete_all

Page 10: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

How to create relations?

Page 11: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Relations

Product Owner

class Product belongs_to :product_ownerend

class Owner has_many :productsend

scb

ssb

marci

id name owner_id

1 scb 1

2 ssb 1

id name

1 marci

products owner

Page 12: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Has and belongs to many

Developer

gyp

class Developer has_and_belongs_to_many :productsend

tia

id name

1 gyp

2 tia

Product

class Product has_one :owner has_and_belongs_to_many :developersend

scb

ssb

id name owner_id

1 scb 1

2 ssb 1

developer_id product_id

1 1

1 2

developers productsdevelopers_products

Page 13: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Questions?

Page 14: Rails Models

Balabit Meetup - June 17, 2010 [email protected]

Thank you!