Basic Rails Projects Professor Larry Heimann Application Design & Development Information Systems Program
Why Ruby on Rails? “Ruby on Rails is astounding. Using it is like watching a kung-fu movie, where a dozen bad-ass frameworks prepare to beat up on the little newcomer only to be handed their asses in a variety of imaginative ways.” — Nathan Torkington O'Reilly Program Chair for OSCON
Automated tasks • rails new • rails generate • rails server • rails db:create • rails db:migrate • rails test:models
Model basics • ActiveRecord does the heavy lifting • Basic relationships and validations • Other methods can be added Key Idea : Models hold all the data & business logic
History of Active Record • Did not originate with Rails! • Design pattern from Martin Fowler’s book Patterns of Enterprise Application Architecture (2002) • “An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.”
Goals of Active Record • Make working with databases easier • Reduce repetition in code • Cut down on configuration needed to make applications work
Migrations
Migration Data Types mysql openbase oracle postgresql sqlite sqlserver db2 :binary blob(32768) blob object blob bytea blob image :boolean decimal(1) tinyint(1) boolean number(1) boolean boolean bit date date date :date date date date datetime datetime datetime date :datetime timestamp timestamp datetime datetime :decimal decimal decimal decimal decimal decimal decimal decimal float float number :float float float float float(8) int(11) integer number(38) :integer int integer integer int varchar(255) char(4096) varchar2(255) :string varchar(255) (note 1) varchar(255) varchar(255) text text clob :text clob(32768) text text text time time date :time time time datetime datetime datetime timestamp date :timestamp timestamp timestamp datetime datetime
Class versus table names
Plurals vs. Singular in Rails OwnersController Pet model owners table has_many :pets belongs_to :owner @owner.pets @pet.owner
Relationships (see examples in PATS)
ActiveRecord creates SQL (examples given in class)
Controllers in charge • Basic actions include: index, show, new, create, edit, update, delete • Controllers get needed data from models first • Actions connected automatically to view pages Key Idea : Controller handle the process logic
All about views • CSS and images • Layouts • Partials Key Idea : Views only know what controllers tell them
Comic of the Day...
pItlh
Recommend
More recommend