Download - 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
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
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)
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
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
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
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”}
Balabit Meetup - June 17, 2010 [email protected]
Destroy
scb = Product.find_by_name “scb”scb.delete
Product.delete_all
Balabit Meetup - June 17, 2010 [email protected]
How to create relations?
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
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
Balabit Meetup - June 17, 2010 [email protected]
Questions?
Balabit Meetup - June 17, 2010 [email protected]
Thank you!