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"

Pro Active Record helps you take advantage of the full power of your database engine from within your Ruby programs and Rails applications. ActiveRecord, part of the magic that makes the Rails framework so powerful and easy to use, is the model element of Rails model/view/controller framework. Its an object-relational mapping library enabling you to interact with databases from both Ruby and Rails applications.

Because ActiveRecord is configured with default assumptions that mesh perfectly with the Rails framework, Rails developers often find they hardly need think about it at all. However, if you are developing in Ruby without Rails, or are deploying against legacy databases designed without Rails in mind, or you just want to take advantage of database-specific features such as large objects and stored procedures, you need the in-depth knowledge of ActiveRecord found in this book.

In Pro Active Record , authors Kevin Marshall, Chad Pytel, and Jon Yurek walk you through every step from the basics of getting and installing the ActiveRecord library to working with legacy schema to using features specific to each of todays most popular database engines, including Oracle, MS SQL, MySQL, and more! You’ll come to a deep understanding of ActiveRecord that will enable you to truly exploit all that Ruby, Rails, and your chosen database platform have to offer.

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

Language: English
Pages: 304

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