1、More Rails: ActiveRecord, ActionController, ActionView, and if time, Associations,Administrivia,Armando/Will office hours moved to Thursdays 1:30 Lab will be staffed for up to 30 mins. after most classes Well try to work more labs into the class time starting next week,Active Record: what is it?,A c
2、lass library that provides an object-relational model over a plain old RDBMS Deal with objects & attributes rather than rows & columns SELECT result rows enumerable collection (later) object graph join query,SQL 101 (Structured Query Language),Relational model of data organization (Codd, 1969) based
3、 on predicate logic & set theory Theoretical model implemented by Gray et al. in 1970s portable language (structured query language, SQL) to express relational operations relational database stores the data and provides transactional semantics to instantiate the abstract relational model Think of a
4、table as an unordered collection of objects that share a schema of simply-typed attributes eg: Student = Think of SELECT as picking some records out SELECT lastname,ucb_sid FROM students WHERE degree_expected 12/31/07 Generally,SELECT attribs FROM tables WHERE constraints Joins are more interesting,
5、 well do them later,While were on SQL. whats a primary key anyway?,Column whose value must be unique for every table row Why not just use (e.g.) last name or SID#? SQL AUTO_INCREMENT function makes it easy to specify an integer primary key If using migrations to create tables (recommended), Rails ta
6、kes care of creating an autoincrement primary key field called ID,CREATE TABLE students ( id INT NOT NULL AUTO_INCREMENT, last_name VARCHAR(255), first_name VARCHAR(255), ucb_sid INT(11) DEFAULT 9999 );,class CreateStudentsfalse, :default=9999 end enddef self.down drop_table :students end end,A Rela
7、tional Database System is a “SQL Server“,Maintains tables, accepts SQL queries, returns results API varies (embedded library, socket, etc.) RDBMS servers maintain ACID properties Atomicity: all or nothing Consistency: like a single copy Isolation: transactions that execute simultaneously & touch sam
8、e tables dont interfere with each other Durability: changes are “sure” once committed Very hard to get engineering for this correct with high performance = Oracle = $,MySQL vs. SQLite,Command line interface Various GUIs: MySQLAdmin, PHPMyAdmin, CocoaMySQL (MacOS).,Local file,Your app,SQLite library,
9、DB#1,DB#n,Server process,Server process,SQL commands,results, , ,filesystem calls,Review: CRUD,4 basic operations on a table row: Create, Read, Update attributes, Destroy INSERT INTO students (last_name, ucb_sid, degree_expected) VALUES (“Fox”, 99999, “1998-12-15”), (“Bodik”, 88888, “2009-06-05”) SE
10、LECT * FROM students WHERE (degree_expected “2000-01-01”) UPDATE students SET degree_expected=“2008-06-05” WHERE last_name=“Bodik”) DELETE FROM students WHERE ucb_sid=99999,More on Student Example,object attributes are “just” instance methods (a la attr_accessor) so can already say stu.last_name, st
11、u.ucb_sid, etc. what line in what file makes this happen? ActiveRecord accessors/mutators default attr_accessor for each table column perform type-casting as needed can be overridden, virtualized, etc.,Example: a short tour,Predicate-like method names often end with question mark,self (like Java thi
12、s) not strictly necessary here,Interpolation of expressions into strings,Some useful class methods of Date,Constructors,Method named initialize, but invoked as new (at least) 3 ways to call it.,New != Create,Call s.save to write the object to the database s.create(args) s.new(args); s.save s.update_
13、attributes(hash) can be used to update attributes in place s.new_record? is true iff no underlying database row corresponds to s save does right thing in SQL (INSERT or UPDATE) Convention over configuration: if id column present, assumes primary key if updated_at/created_at columns in table, automat
14、ically are set to update/creation timestamp,find() SQL SELECT,# To find an arbitrary single record: s = Student.find(:first) # returns a Student instance # To find all records: students = Student.find(:all) # returns enumerable!# find by id primary key (Note! throws RecordNotFound) book = Book.find(
15、1235) # Find a whole bunch of things ids_array = get_list_of_ids_from_somewhere() students = Student.find(ids_array)# To find by column values: armando = Student.find_by_last_name(Fox) # may return nil a_local_grad = Student.find_by_city_and_degree_expected(Berkeley, Date.parse(June 15,2007)# To fin
16、d only a few, and sort by an attribute many_localgrads = Student.find_all_by_city_and_degree_expected(Berkeley, Date.parse(June 15,2007),:limit=30,:order=:last_name) ,Find by conditions,Use ? for values from parameters. Rails will sanitize the SQL and prevent any SQL injectionYou will want to learn
17、some minimal SQL syntax,# Using SQL conditions books = Book.find(:all, :conditions = pub_date between ? and ?, params:start_date, params:end_date, :order = pub_date DESC),You can also specify ordering and use arbitrary SQL operators:,Find by conditions,Use ? to substitute in condition values not man
18、datory, but a good idea!You can include other SQL functionalityYou can roll your own s = Student.find_by_sql(“SELECT * FROM students .“),# Using SQL conditions books = Book.find(:all, :conditions = pub_date between ? and ?, params:start_date, params:end_date, :order = pub_date DESC),Advanced Find,bo
19、oks = Book.find(:all, :conditions = pub_date between ? and ?, params:start_date, params:end_date,:limit = 10, :offset = params:page.to_i * 10),You can also specify limits and offsets, and oh so much more,:lock - Holds lock on the records (default: share lock) :select - Specifies columns for SELECT (
20、default *) :group - (used with select) to group :readonly - load as read-only (object cant be saved) :include - Prefetches joined tables (try :include first; more about this in Section 4) Note: use SQL-specific features at your own risk,Caveat!,The result of a find-all operation mixes in Enumerable
21、Enumerable defines methods find and find_all Not to be confused with ActiveRecord:Base#find!,Action View,A template for rendering views of the model that allows some code embedding commonly RHTML (.html.erb); also RXML, HAML, RJS note.too much code breaks MVC separation convention: views for model f
22、oo are in app/views/foo/ “Helper methods” for interacting with models model valuesHTML elements (e.g. menus) HTML form inputassignment to model objects DRY (Dont Repeat Yourself) support Layouts capture common page content at application level, model level, etc. (app/views/layouts/) Partials capture
23、 reusable/parameterizable view patterns,Helper Methods for Input & Output,Review: we saw a simple view already. Anatomy: But these form tags are generic.what about model-specific form tags? In the RHTML template:.etc In HTML delivered to browser:What happened?,Action Controller,Each incoming request
24、 instantiates a new Controller object with its own instance variables Routing (Sec. 4) determines which method to call Parameter unmarshaling (from URL or form sub.) into params hash .well, not really a hash.but responds to , = Controller methods set up instance variables these will be visible to th
25、e view controller has access to models class methods; idiomatically, often begins with Model.find(.) Lets see some examples.,Then we render.,Once logic is done, render the viewexactly one render permitted from controller method (1 HTTP request 1 response) Convention over configuration: implicit rend
26、er if no other render specified explicitly in action method looks for template matching controller method name and renders with default layouts (model, app),What about those model-specific form elements?,Recall: Related form elements for student attributes will be named studentattr marshalled into p
27、arams as params:student:last_name, params:student:degree_expected, etc. i.e, params:student is a hash :last_name=string, :degree_expected=date, etc. and can be assigned directly to model object instance helpers for dates and other “complex” types.magic,What else can happen?,redirect_to allows fallin
28、g through to different action without first rendering fallthrough action will call render instead works using HTTP 302 Found mechanism, i.e. separate browser roundtrip example: update method fail: render the edit action again success: redirect to “URL indicated by this student object” alternate (old
29、er) syntax for redirects: redirect_to :action = show, :id = student.id,The Session Hash,Problem: HTTP is stateless (every request totally independent). How to synthesize a session (sequence of related actions) by one user? Rails answer: session is a magic persistent hash available to controller Actu
30、ally, its not really a hash, but it quacks like one Managed at dispatch level using cookies You can keep full-blown objects there, or just ids (primary keys) of database records Deploy-time flag lets sessions be stored in filesystem, DB table, or distributed in-memory hash table,The Flash,Problem: I
31、m about to redirect_to somewhere, but want to display a notice to the user yet that will be a different controller instance with all new instance variables Rails answer: flash contents are passed to the next action, then cleared to this action: flash.now:notice visible to views as well as controller
32、,Strictly speaking, could use session & clear it out yourself,Intro. to Associations,Lets define a new model to represent Courses. keep it simple: name, CCN, start date (month & year) Whats missing: a way to identify who is in the class! Rails solution: (similar to database foreign keys) Add column
33、course_id to Students table Declare that a Course has_many :students Both of these are Rails conventions Set a given students course_id field for the course they are taking An obvious problem with this approach.but well fix it later,Associations In General,x has_many y the y table has an x_id column
34、 y belongs_to x Note! Table structure unaffected by whether you also define the belongs_toso why do it? x has_one y actually like has_many: does same SQL query but returns only the first result row,Using Associations,Going forward (course has_many students): c = Course.find(.) s = c.students What is
35、 the “type” of s? Going the other way (student belongs_to course): s = Student.find(.) c = s.course,Modeling professors,How should we change the schema to support course belongs_to professor?,What about all the students that a professor teaches?,p = Professor.find(.) c = Professor.courses s = c.stud
36、ents OrNow we can just write:s = Professor.find(.).students,What is happening in terms of tables in this example?,SQL is doing a join Which youll learn about next time The message is: Active Record tries to provide the abstraction of an object graph by using SQL table joins. The xxx_id fields are ca
37、lled foreign keys.,Remember,has_many, etc. are not part of the language nor are they macros just regular old methods! How do they work?,Learn more on your own,These links will be posted on wiki soon SQLite: http:/ General SQL intro: http:/ Associations: Agile Rails book Ch. 14,Summary,ActiveRecord p
38、rovides (somewhat-)database-independent object model over RDBMS ActionView supports display & input of model objects facilitates reuse of templates via layouts & partials ActionController dispatches user actions, manipulates models, sets up variables for views declarative specifications capture common patterns for checking predicates before executing handlers,Virtual attributes example: simple authentication,Assume we have a table customers with columns salt and hashed_password.,Defines the receiver method for password=,Why do we want to use self here?,Wheres the accessor for password?,
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1