Rails Models

Post on 25-Jun-2015

1.137 views 0 download

Tags:

Transcript of Rails Models

How to create relationshipsbetween models

June 17, 2010nucc@balabit.com

MeetUP @ Balabit

Balabit Meetup - June 17, 2010 nucc@balabit.com

Model View Controller

Model ViewController

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

HTML, XML, JSON, CSV, ...

router

Balabit Meetup - June 17, 2010 nucc@balabit.com

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

Balabit Meetup - June 17, 2010 nucc@balabit.com

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)

Balabit Meetup - June 17, 2010 nucc@balabit.com

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

Balabit Meetup - June 17, 2010 nucc@balabit.com

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

Balabit Meetup - June 17, 2010 nucc@balabit.com

Fetch information

class Product

id name

1 scb

2 syslog-ng

3 zorp

4 ssb

...

Table productsproduct = find_by_name(“scb”)

product.id# 1

Balabit Meetup - June 17, 2010 nucc@balabit.com

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”}

Balabit Meetup - June 17, 2010 nucc@balabit.com

Destroy

scb = Product.find_by_name “scb”scb.delete

Product.delete_all

Balabit Meetup - June 17, 2010 nucc@balabit.com

How to create relations?

Balabit Meetup - June 17, 2010 nucc@balabit.com

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

Balabit Meetup - June 17, 2010 nucc@balabit.com

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

Balabit Meetup - June 17, 2010 nucc@balabit.com

Questions?

Balabit Meetup - June 17, 2010 nucc@balabit.com

Thank you!