More On Active Records - PowerPoint PPT Presentation

About This Presentation
Title:

More On Active Records

Description:

Certain validators can be run on. Create only. Update only ... Above validators can be written using helpers. Several of these validation helpers are provided ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 15
Provided by: venkatsub
Learn more at: https://www2.cs.uh.edu
Category:

less

Transcript and Presenter's Notes

Title: More On Active Records


1
More On Active Records
2
Foreign Keys
  • Active Records needs help with relationships
  • One to one
  • One to many
  • Many to many
  • The foreign keys are specified, by convention,
    using column names in the form of model name
    appended with _id
  • For example graduate_id

3
Specifying Relationships
  • You can specify using
  • has_one
  • has_many
  • belongs_to
  • has_and_belongs_to_many
  • These specify relationships and add useful
    methods
  • belongs_to causes the object related to be saved
    appropriately
  • depend gt true in has_one causes the related
    object to be deleted along with containing object

4
Performance of Queries
  • By default, of queries increases when you
    access objects with relationships
  • One query to get object
  • additional queries to get related objects
  • Often called n1 problem
  • You can hint Active Records to pre-fetch in the
    find method
  • May not always improve performance
  • DB Required to support left outer joints
  • May bring large amount of unwanted data (in some
    cases) and unnecessary conversion to objects

5
Transactions
  • transaction method bounds operations
  • Protects database
  • But not object modified in memory

6
Transactions
  • You can send bound objects to transaction method
  • Restores the objects if transaction fails

7
Aggregation
  • Allows you to map certain columns of your table
    as an object within your object
  • composed_of attr_name, class_name gt ClassName,
    mapping gt mapping

8
Inheritance
  • OO world uses inheritance
  • Relational relies on association
  • Single table inheritance is an approach

type
id
9
Validation
  • Contents of object can be checked for validity
    during save
  • Certain validators can be run on
  • Create only
  • Update only
  • Save (create and update)
  • validate
  • validate_on_create
  • validate_on_update
  • You can explicitly validate by calling valid?()
    method
  • errors holds any validation errors
  • errors.on(symbol)
  • errors.clear()

10
Validation
11
Helpers
  • Above validators can be written using helpers
  • Several of these validation helpers are provided

12
Callback
  • Active Records monitors objects through objects
    lifecycle
  • It gives us opportunity to do special operations
  • We will make good use of this to do things like
    encrypting of data before save
  • You can provide these as methods or as handlers
  • Except after_find and after_initialize which are
    required to be methods

13
Callback
14
Observers
  • You may pollute your model with handlers
  • Observe can make for a better cohesion
Write a Comment
User Comments (0)
About PowerShow.com