User.create! row.to_hash
end
Schema operations
ActiveRecord??™s migration methods can be used as a portable abstraction for SQL
data definition language (DDL). The ActiveRecord::Schema.define function allows
you to use the ActiveRecord schema definition statements within a block to perform
operations on a database. The full set of DDL operations is documented in
ActiveRecord::ConnectionAdapters::SchemaStatements.
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
# (connection spec)...
)
ActiveRecord::Schema.define do
create_table :sites do |t|
t.string :name, :null => false
t.string :city
t.string :state
end
add_column :users, :site_id, :integer
add_index :users, :site_id
end
286 | Chapter 9: Incorporating and Extending Rails
Standalone data store
Often, a console or desktop application needs to store persistent data, whether it be
preference data or application data itself. A common solution is to use YAML, which
can marshal and unmarshal most Ruby objects (round trip), while also being humanreadable.
However, YAML is verbose compared to binary data formats, which may
be an issue when storing larger amounts of data. SOAP::Marshal from Ruby??™s standard
library is similar; it can serialize objects into an (often quite verbose) XML representation.
This approach has similar benefits and drawbacks to YAML.
Another option is to use Ruby??™s Marshal module, which dumps Ruby objects into a
more concise byte stream.
Pages:
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434