Pro Active Record: Databases with Ruby and Rails

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

I have to wonder if people that buy the "for idiots" series of books expect that they were really written "for idiots"? Just because this book is called "pro", does not mean that it's some super high-level diatribe about active record. If you're such a pro, just read the source code for active record, all the docs you need are in there. If you want to actually see some examples and read a fair discussion, this book will suite you well. The worst thing about this volume, and most others from APRESS is that if you want an eBook version you have to pay additional fees and then get a "locked" pdf of the book. Wake up, APRESS - get with the Kindle program.

Author(s): Kevin Marshall, Chad Pytel, Jon Yurek
Edition: 1
Publisher: Apress
Year: 2007

Language: English
Pages: 294

Cover......Page 1
Contents at a Glance......Page 5
Contents......Page 6
About the Authors......Page 13
About the Technical Reviewer......Page 14
Acknowledgments......Page 15
Introduction......Page 16
1 Introducing Active Record......Page 18
Active Record Mostly Adheres to the ORM Pattern......Page 19
Active Record Is a Different Kind of ORM......Page 20
Active Record Is Primarily Used for CRUD Database Transactions......Page 21
From Active Record Objects to Database Records and Back Again......Page 22
Saving the Attributes as a Record in the Database......Page 23
Why Active Record Is a Smart Choice......Page 24
Installing the Active Record Gem......Page 25
Installing Any Additional Required Libraries or Gems......Page 26
Supplying the Adapter-Specific Information......Page 27
Firebird Parameters......Page 28
MySQL Parameters......Page 29
OpenBase Parameters......Page 30
PostgreSQL Parameters......Page 31
SQL Server Parameters......Page 32
Learning More......Page 33
Your First Example......Page 35
Active Record Assumptions and Conventions......Page 36
Overriding the Assumptions......Page 37
Retrieving Objects from the Database......Page 38
Exploring Active Record Relationships......Page 39
Them’s the Basics!......Page 41
Creating a Record......Page 42
Reading a Record......Page 44
:conditions......Page 45
:include......Page 46
:group......Page 48
:limit and :offset......Page 49
Dynamic Finders......Page 50
Updating a Record......Page 51
Deleting a Record......Page 52
Completely Nondynamic Finders......Page 54
Transactions......Page 55
Locking......Page 57
Optimistic Locking......Page 58
CRUD Isn’t Cruddy......Page 59
Designing Active Record–Friendly Tables......Page 60
Traditional Database Management......Page 61
Common Problems with the Traditional Approach......Page 62
How the DSL Works......Page 63
Using Migrations......Page 64
Executing Migrations Within Ruby on Rails......Page 65
Executing Migrations Outside of Ruby on Rails......Page 66
Migrations in Action......Page 67
Creating Tables......Page 68
Adding, Removing, and Changing Database Columns......Page 70
Indexing Columns......Page 71
Managing Application Data......Page 72
Migrations Are Easier Than They Sound......Page 74
Callbacks......Page 75
Implementing Callbacks......Page 76
Callback Macros......Page 77
after_find......Page 79
after_save......Page 80
before_update......Page 81
after_validation......Page 82
before_validation_on_update......Page 83
after_destroy......Page 84
Farmers, Cows, Milk, and How They Relate......Page 85
belongs_to......Page 86
has_many......Page 87
has_one......Page 88
has_and_belongs_to_many......Page 90
has_many :through......Page 91
Association Modifiers......Page 92
:foreign_key......Page 93
:polymorphic and :as......Page 94
:dependent......Page 95
Why Bother with Validations?......Page 96
Implementing Validations......Page 97
validates_each......Page 99
validates_confirmation_of......Page 100
validates_presence_of......Page 101
validates_uniqueness_of......Page 102
validates_associated......Page 103
validates_numericality_of......Page 104
Your Core Is Strong......Page 105
Active Record Observers......Page 106
Canned Functionality......Page 107
Step 1: Setting Up Your Associations......Page 108
Step 3: Adding the acts_as_list Method......Page 109
What acts_as_list Gives You......Page 110
Acting as a Tree......Page 112
Step 2: Defining Your acts_as_tree Method......Page 113
What acts_as_tree Gives You......Page 114
Step 2: Defining Your acts_as_nested Method......Page 116
What acts_as_nested_set Gives You......Page 118
Aggregations......Page 120
The Name of Your Value Object......Page 121
Step 2: Defining Your Value Object......Page 122
Putting It All Together: Using Aggregations......Page 123
Extending Active Record the Easy Way......Page 124
Writing Code That Writes Code......Page 125
Meet method_missing......Page 127
What Column Did You Want, Again?......Page 131
But What About the Farmer?......Page 132
Adding Class Methods......Page 135
Don’t Shoot Yourself in the Foot......Page 138
Unit Testing......Page 139
Why Write Unit Tests?......Page 140
How to Write Good Unit Tests......Page 141
assert......Page 143
assert_nil......Page 144
assert_equal......Page 145
assert_raise......Page 146
assert_instance_of......Page 147
assert_respond_to......Page 148
assert_same......Page 149
assert_operator......Page 150
assert_throws......Page 151
flunk......Page 152
Benefits of Fixtures......Page 153
Transaction Support with Fixtures......Page 155
CSV......Page 156
Single File......Page 157
Active Record Error Methods......Page 158
SubclassNotFound......Page 159
AssociationTypeMismatch......Page 160
AdapterNotSpecified......Page 161
ConnectionFailed......Page 162
StatementInvalid......Page 163
PreparedStatementInvalid......Page 164
StaleObjectError......Page 165
AttributeAssignmentError......Page 166
Active Record and Logging......Page 167
Active Record Benchmarking......Page 173
Testing Is Fun!......Page 174
7 Working with Legacy Schema......Page 175
How Much Do You Want to Do in Active Record?......Page 176
Is There an Easier or More Efficient Way?......Page 177
primary_key_prefix_type......Page 178
table_name_prefix......Page 179
pluralize_table_names......Page 180
default_timezone......Page 181
allow_concurrency......Page 182
schema_format......Page 183
set_table_name......Page 184
set_Inheritance_column......Page 185
set_sequence_name......Page 186
Making the Complex Easier......Page 187
CRUD Operations and Complex SQL Statements......Page 189
Improving Performance and Cutting Out the Middle Man......Page 191
Stored Procedures, Custom Functions, and Sequences......Page 193
Importing and Exporting......Page 195
Exporting XML......Page 196
Importing XML......Page 197
Importing YAML......Page 198
Importing CSV......Page 199
You’re on Your Way to Becoming a Legend......Page 200
Exploring Active Record Source Code......Page 201
Walking Through active_record.rb......Page 202
Walking Through base.rb......Page 203
Putting It All Back Together......Page 205
The Keys to the Enterprise......Page 206
Two Steps Forward, One Step Back......Page 207
A World of Resources......Page 208
Adding Your Own Two Cents......Page 209
The Cons of DBI......Page 210
Og......Page 211
An Og Example......Page 212
Database-Specific Libraries......Page 213
The Cons of Active Resource......Page 214
How Do I Use Multiple Databases with Active Record?......Page 215
How Do I Handle Internationalization and Localization?......Page 218
How Do I Use GUID/UUID Primary Keys?......Page 219
What Database Locking Mechanisms Does Active Record Support?......Page 220
How Do I Select a Random Record from the Database?......Page 221
with Active Record?......Page 222
How Do I Properly Use find_by_sql?......Page 224
How Do I Ensure that All My Records Are Valid?......Page 225
Can I Use the Same Name for a Database Column and an Active Record Model?......Page 226
How Can You Paginate Active Record Results?......Page 227
Where Can I Get More Active Record Help?......Page 228
attr_accessible(*attributes)......Page 229
columns_hash()......Page 230
delete(id)......Page 231
find(*args)......Page 232
new(attributes=nil) { |self if block_given?| . . . }......Page 234
serialized_attributes()......Page 235
set_table_name(value = nil, &block)......Page 236
update_all(update_sql, conditions)......Page 237
compute_type(type_name)......Page 238
attributes(options = nil)......Page 239
decrement(attribute)......Page 240
id=(value)......Page 241
save()......Page 242
to_xml(options = {})......Page 243
toggle!(attribute)......Page 244
calculate(operation, column_name, options = {})......Page 245
minimum(column_name, options = {})......Page 246
after_validation_on_create()......Page 247
before_validation_on_update()......Page 248
supports_migrations?()......Page 249
string_to_dummy_time(value)......Page 250
number?()......Page 251
begin_db_transaction()......Page 252
select_one(sql, name = nil)......Page 253
quote_string(value)......Page 254
add_index(table_name, column_name, options = {})......Page 255
create_table(name, options = {}) { || . . . }a......Page 256
initialize_schema_information()......Page 257
table_alias_for(table_name)......Page 258
column(name, type, options = {})......Page 259
[](attribute)......Page 260
count()......Page 261
size()......Page 262
write(text="")......Page 263
observers=(*observers)......Page 264
reflections()......Page 265
define(info = {}, &block)......Page 266
transaction(*objects, &block)......Page 267
valid?()......Page 268
validate(*methods, &block)......Page 269
validate_on_create(*methods, &block)......Page 270
validates_acceptance_of(*attribute_names)......Page 271
validates_confirmation_of(*attribute_names)......Page 272
validates_each(*attribute_names) { |record, attribute, value| . . . }......Page 273
validates_exclusion_of(*attribute_names)......Page 274
validates_format_of(*attribute_names)......Page 275
validates_length_of(*attribute_names)......Page 276
validates_numericality_of(*attribute_names)......Page 277
validates_presence_of(*attribute_names)......Page 278
validates_uniqueness_of(*attribute_names)......Page 279
Index......Page 281