activerecord 1.0.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +213 -0
- data/examples/performance.rb +172 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +180 -84
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +248 -0
- data/lib/active_record/associations/association_scope.rb +135 -0
- data/lib/active_record/associations/belongs_to_association.rb +92 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +35 -0
- data/lib/active_record/associations/builder/association.rb +108 -0
- data/lib/active_record/associations/builder/belongs_to.rb +98 -0
- data/lib/active_record/associations/builder/collection_association.rb +89 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +25 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +608 -0
- data/lib/active_record/associations/collection_proxy.rb +986 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +58 -39
- data/lib/active_record/associations/has_many_association.rb +116 -85
- data/lib/active_record/associations/has_many_through_association.rb +197 -0
- data/lib/active_record/associations/has_one_association.rb +102 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +235 -0
- data/lib/active_record/associations/join_helper.rb +45 -0
- data/lib/active_record/associations/preloader/association.rb +121 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +63 -0
- data/lib/active_record/associations/preloader.rb +178 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +1437 -431
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +70 -0
- data/lib/active_record/attribute_methods/dirty.rb +118 -0
- data/lib/active_record/attribute_methods/primary_key.rb +122 -0
- data/lib/active_record/attribute_methods/query.rb +40 -0
- data/lib/active_record/attribute_methods/read.rb +107 -0
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -0
- data/lib/active_record/attribute_methods/write.rb +63 -0
- data/lib/active_record/attribute_methods.rb +393 -0
- data/lib/active_record/autosave_association.rb +426 -0
- data/lib/active_record/base.rb +268 -930
- data/lib/active_record/callbacks.rb +203 -230
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +638 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +390 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +129 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +501 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +873 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +389 -275
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
- data/lib/active_record/connection_adapters/column.rb +318 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +517 -90
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +911 -138
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +624 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +463 -0
- data/lib/active_record/counter_cache.rb +122 -0
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +213 -0
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +892 -138
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +47 -0
- data/lib/active_record/locking/optimistic.rb +181 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +82 -0
- data/lib/active_record/migration/command_recorder.rb +164 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +1015 -0
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +546 -0
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +509 -0
- data/lib/active_record/query_cache.rb +56 -0
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +205 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +50 -0
- data/lib/active_record/railties/databases.rake +402 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +30 -0
- data/lib/active_record/reflection.rb +544 -87
- data/lib/active_record/relation/batches.rb +93 -0
- data/lib/active_record/relation/calculations.rb +399 -0
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +349 -0
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +106 -0
- data/lib/active_record/relation/query_methods.rb +1044 -0
- data/lib/active_record/relation/spawn_methods.rb +73 -0
- data/lib/active_record/relation.rb +655 -0
- data/lib/active_record/result.rb +67 -0
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +168 -0
- data/lib/active_record/schema.rb +65 -0
- data/lib/active_record/schema_dumper.rb +204 -0
- data/lib/active_record/schema_migration.rb +39 -0
- data/lib/active_record/scoping/default.rb +146 -0
- data/lib/active_record/scoping/named.rb +175 -0
- data/lib/active_record/scoping.rb +82 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/serializers/xml_serializer.rb +197 -0
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +156 -0
- data/lib/active_record/tasks/database_tasks.rb +203 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +96 -0
- data/lib/active_record/timestamp.rb +119 -0
- data/lib/active_record/transactions.rb +366 -69
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +49 -0
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +225 -0
- data/lib/active_record/validations.rb +64 -185
- data/lib/active_record/version.rb +11 -0
- data/lib/active_record.rb +149 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +62 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +39 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record.rb +23 -0
- metadata +261 -161
- data/CHANGELOG +0 -581
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.png +0 -0
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/associations/association_collection.rb +0 -70
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -107
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/observer.rb +0 -71
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2004-2013 David Heinemeier Hansson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
= Active Record -- Object-relational mapping put on rails
|
2
|
+
|
3
|
+
Active Record connects classes to relational database tables to establish an
|
4
|
+
almost zero-configuration persistence layer for applications. The library
|
5
|
+
provides a base class that, when subclassed, sets up a mapping between the new
|
6
|
+
class and an existing table in the database. In the context of an application,
|
7
|
+
these classes are commonly referred to as *models*. Models can also be
|
8
|
+
connected to other models; this is done by defining *associations*.
|
9
|
+
|
10
|
+
Active Record relies heavily on naming in that it uses class and association
|
11
|
+
names to establish mappings between respective database tables and foreign key
|
12
|
+
columns. Although these mappings can be defined explicitly, it's recommended
|
13
|
+
to follow naming conventions, especially when getting started with the
|
14
|
+
library.
|
15
|
+
|
16
|
+
A short rundown of some of the major features:
|
17
|
+
|
18
|
+
* Automated mapping between classes and tables, attributes and columns.
|
19
|
+
|
20
|
+
class Product < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
|
23
|
+
The Product class is automatically mapped to the table named "products",
|
24
|
+
which might look like this:
|
25
|
+
|
26
|
+
CREATE TABLE products (
|
27
|
+
id int(11) NOT NULL auto_increment,
|
28
|
+
name varchar(255),
|
29
|
+
PRIMARY KEY (id)
|
30
|
+
);
|
31
|
+
|
32
|
+
This would also define the following accessors: `Product#name` and
|
33
|
+
`Product#name=(new_name)`
|
34
|
+
|
35
|
+
{Learn more}[link:classes/ActiveRecord/Base.html]
|
36
|
+
|
37
|
+
|
38
|
+
* Associations between objects defined by simple class methods.
|
39
|
+
|
40
|
+
class Firm < ActiveRecord::Base
|
41
|
+
has_many :clients
|
42
|
+
has_one :account
|
43
|
+
belongs_to :conglomerate
|
44
|
+
end
|
45
|
+
|
46
|
+
{Learn more}[link:classes/ActiveRecord/Associations/ClassMethods.html]
|
47
|
+
|
48
|
+
|
49
|
+
* Aggregations of value objects.
|
50
|
+
|
51
|
+
class Account < ActiveRecord::Base
|
52
|
+
composed_of :balance, class_name: 'Money',
|
53
|
+
mapping: %w(balance amount)
|
54
|
+
composed_of :address,
|
55
|
+
mapping: [%w(address_street street), %w(address_city city)]
|
56
|
+
end
|
57
|
+
|
58
|
+
{Learn more}[link:classes/ActiveRecord/Aggregations/ClassMethods.html]
|
59
|
+
|
60
|
+
|
61
|
+
* Validation rules that can differ for new or existing objects.
|
62
|
+
|
63
|
+
class Account < ActiveRecord::Base
|
64
|
+
validates :subdomain, :name, :email_address, :password, presence: true
|
65
|
+
validates :subdomain, uniqueness: true
|
66
|
+
validates :terms_of_service, acceptance: true, on: :create
|
67
|
+
validates :password, :email_address, confirmation: true, on: :create
|
68
|
+
end
|
69
|
+
|
70
|
+
{Learn more}[link:classes/ActiveRecord/Validations.html]
|
71
|
+
|
72
|
+
|
73
|
+
* Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.).
|
74
|
+
|
75
|
+
class Person < ActiveRecord::Base
|
76
|
+
before_destroy :invalidate_payment_plan
|
77
|
+
# the `invalidate_payment_plan` method gets called just before Person#destroy
|
78
|
+
end
|
79
|
+
|
80
|
+
{Learn more}[link:classes/ActiveRecord/Callbacks.html]
|
81
|
+
|
82
|
+
|
83
|
+
* Inheritance hierarchies.
|
84
|
+
|
85
|
+
class Company < ActiveRecord::Base; end
|
86
|
+
class Firm < Company; end
|
87
|
+
class Client < Company; end
|
88
|
+
class PriorityClient < Client; end
|
89
|
+
|
90
|
+
{Learn more}[link:classes/ActiveRecord/Base.html]
|
91
|
+
|
92
|
+
|
93
|
+
* Transactions.
|
94
|
+
|
95
|
+
# Database transaction
|
96
|
+
Account.transaction do
|
97
|
+
david.withdrawal(100)
|
98
|
+
mary.deposit(100)
|
99
|
+
end
|
100
|
+
|
101
|
+
{Learn more}[link:classes/ActiveRecord/Transactions/ClassMethods.html]
|
102
|
+
|
103
|
+
|
104
|
+
* Reflections on columns, associations, and aggregations.
|
105
|
+
|
106
|
+
reflection = Firm.reflect_on_association(:clients)
|
107
|
+
reflection.klass # => Client (class)
|
108
|
+
Firm.columns # Returns an array of column descriptors for the firms table
|
109
|
+
|
110
|
+
{Learn more}[link:classes/ActiveRecord/Reflection/ClassMethods.html]
|
111
|
+
|
112
|
+
|
113
|
+
* Database abstraction through simple adapters.
|
114
|
+
|
115
|
+
# connect to SQLite3
|
116
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'dbfile.sqlite3')
|
117
|
+
|
118
|
+
# connect to MySQL with authentication
|
119
|
+
ActiveRecord::Base.establish_connection(
|
120
|
+
adapter: 'mysql2',
|
121
|
+
host: 'localhost',
|
122
|
+
username: 'me',
|
123
|
+
password: 'secret',
|
124
|
+
database: 'activerecord'
|
125
|
+
)
|
126
|
+
|
127
|
+
{Learn more}[link:classes/ActiveRecord/Base.html] and read about the built-in support for
|
128
|
+
MySQL[link:classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html],
|
129
|
+
PostgreSQL[link:classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html], and
|
130
|
+
SQLite3[link:classes/ActiveRecord/ConnectionAdapters/SQLite3Adapter.html].
|
131
|
+
|
132
|
+
|
133
|
+
* Logging support for Log4r[http://log4r.rubyforge.org] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc].
|
134
|
+
|
135
|
+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
|
136
|
+
ActiveRecord::Base.logger = Log4r::Logger.new('Application Log')
|
137
|
+
|
138
|
+
|
139
|
+
* Database agnostic schema management with Migrations.
|
140
|
+
|
141
|
+
class AddSystemSettings < ActiveRecord::Migration
|
142
|
+
def up
|
143
|
+
create_table :system_settings do |t|
|
144
|
+
t.string :name
|
145
|
+
t.string :label
|
146
|
+
t.text :value
|
147
|
+
t.string :type
|
148
|
+
t.integer :position
|
149
|
+
end
|
150
|
+
|
151
|
+
SystemSetting.create name: 'notice', label: 'Use notice?', value: 1
|
152
|
+
end
|
153
|
+
|
154
|
+
def down
|
155
|
+
drop_table :system_settings
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
{Learn more}[link:classes/ActiveRecord/Migration.html]
|
160
|
+
|
161
|
+
|
162
|
+
== Philosophy
|
163
|
+
|
164
|
+
Active Record is an implementation of the object-relational mapping (ORM)
|
165
|
+
pattern[http://www.martinfowler.com/eaaCatalog/activeRecord.html] by the same
|
166
|
+
name described by Martin Fowler:
|
167
|
+
|
168
|
+
"An object that wraps a row in a database table or view,
|
169
|
+
encapsulates the database access, and adds domain logic on that data."
|
170
|
+
|
171
|
+
Active Record attempts to provide a coherent wrapper as a solution for the inconvenience that is
|
172
|
+
object-relational mapping. The prime directive for this mapping has been to minimize
|
173
|
+
the amount of code needed to build a real-world domain model. This is made possible
|
174
|
+
by relying on a number of conventions that make it easy for Active Record to infer
|
175
|
+
complex relations and structures from a minimal amount of explicit direction.
|
176
|
+
|
177
|
+
Convention over Configuration:
|
178
|
+
* No XML-files!
|
179
|
+
* Lots of reflection and run-time extension
|
180
|
+
* Magic is not inherently a bad word
|
181
|
+
|
182
|
+
Admit the Database:
|
183
|
+
* Lets you drop down to SQL for odd cases and performance
|
184
|
+
* Doesn't attempt to duplicate or replace data definitions
|
185
|
+
|
186
|
+
|
187
|
+
== Download and installation
|
188
|
+
|
189
|
+
The latest version of Active Record can be installed with RubyGems:
|
190
|
+
|
191
|
+
% [sudo] gem install activerecord
|
192
|
+
|
193
|
+
Source code can be downloaded as part of the Rails project on GitHub:
|
194
|
+
|
195
|
+
* https://github.com/rails/rails/tree/master/activerecord
|
196
|
+
|
197
|
+
|
198
|
+
== License
|
199
|
+
|
200
|
+
Active Record is released under the MIT license:
|
201
|
+
|
202
|
+
* http://www.opensource.org/licenses/MIT
|
203
|
+
|
204
|
+
|
205
|
+
== Support
|
206
|
+
|
207
|
+
API documentation is at:
|
208
|
+
|
209
|
+
* http://api.rubyonrails.org
|
210
|
+
|
211
|
+
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
|
212
|
+
|
213
|
+
* https://github.com/rails/rails/issues
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require File.expand_path('../../../load_paths', __FILE__)
|
2
|
+
require "active_record"
|
3
|
+
require 'benchmark/ips'
|
4
|
+
|
5
|
+
TIME = (ENV['BENCHMARK_TIME'] || 20).to_i
|
6
|
+
RECORDS = (ENV['BENCHMARK_RECORDS'] || TIME*1000).to_i
|
7
|
+
|
8
|
+
conn = { :adapter => 'sqlite3', :database => ':memory:' }
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(conn)
|
11
|
+
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
connection.create_table :users, :force => true do |t|
|
14
|
+
t.string :name, :email
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
|
18
|
+
has_many :exhibits
|
19
|
+
end
|
20
|
+
|
21
|
+
class Exhibit < ActiveRecord::Base
|
22
|
+
connection.create_table :exhibits, :force => true do |t|
|
23
|
+
t.belongs_to :user
|
24
|
+
t.string :name
|
25
|
+
t.text :notes
|
26
|
+
t.timestamps
|
27
|
+
end
|
28
|
+
|
29
|
+
belongs_to :user
|
30
|
+
|
31
|
+
def look; attributes end
|
32
|
+
def feel; look; user.name end
|
33
|
+
|
34
|
+
def self.with_name
|
35
|
+
where("name IS NOT NULL")
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.with_notes
|
39
|
+
where("notes IS NOT NULL")
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.look(exhibits) exhibits.each { |e| e.look } end
|
43
|
+
def self.feel(exhibits) exhibits.each { |e| e.feel } end
|
44
|
+
end
|
45
|
+
|
46
|
+
puts 'Generating data...'
|
47
|
+
|
48
|
+
module ActiveRecord
|
49
|
+
class Faker
|
50
|
+
LOREM = %Q{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
|
51
|
+
Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
|
52
|
+
Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
|
53
|
+
tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
|
54
|
+
varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
|
55
|
+
tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
|
56
|
+
Praesent varius tincidunt commodo}.split
|
57
|
+
|
58
|
+
def self.name
|
59
|
+
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join ' '
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.email
|
63
|
+
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join('@') + ".com"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# pre-compute the insert statements and fake data compilation,
|
69
|
+
# so the benchmarks below show the actual runtime for the execute
|
70
|
+
# method, minus the setup steps
|
71
|
+
|
72
|
+
# Using the same paragraph for all exhibits because it is very slow
|
73
|
+
# to generate unique paragraphs for all exhibits.
|
74
|
+
notes = ActiveRecord::Faker::LOREM.join ' '
|
75
|
+
today = Date.today
|
76
|
+
|
77
|
+
puts "Inserting #{RECORDS} users and exhibits..."
|
78
|
+
RECORDS.times do
|
79
|
+
user = User.create(
|
80
|
+
:created_at => today,
|
81
|
+
:name => ActiveRecord::Faker.name,
|
82
|
+
:email => ActiveRecord::Faker.email
|
83
|
+
)
|
84
|
+
|
85
|
+
Exhibit.create(
|
86
|
+
:created_at => today,
|
87
|
+
:name => ActiveRecord::Faker.name,
|
88
|
+
:user => user,
|
89
|
+
:notes => notes
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
Benchmark.ips(TIME) do |x|
|
94
|
+
ar_obj = Exhibit.find(1)
|
95
|
+
attrs = { :name => 'sam' }
|
96
|
+
attrs_first = { :name => 'sam' }
|
97
|
+
attrs_second = { :name => 'tom' }
|
98
|
+
exhibit = {
|
99
|
+
:name => ActiveRecord::Faker.name,
|
100
|
+
:notes => notes,
|
101
|
+
:created_at => Date.today
|
102
|
+
}
|
103
|
+
|
104
|
+
x.report("Model#id") do
|
105
|
+
ar_obj.id
|
106
|
+
end
|
107
|
+
|
108
|
+
x.report 'Model.new (instantiation)' do
|
109
|
+
Exhibit.new
|
110
|
+
end
|
111
|
+
|
112
|
+
x.report 'Model.new (setting attributes)' do
|
113
|
+
Exhibit.new(attrs)
|
114
|
+
end
|
115
|
+
|
116
|
+
x.report 'Model.first' do
|
117
|
+
Exhibit.first.look
|
118
|
+
end
|
119
|
+
|
120
|
+
x.report("Model.all limit(100)") do
|
121
|
+
Exhibit.look Exhibit.limit(100)
|
122
|
+
end
|
123
|
+
|
124
|
+
x.report "Model.all limit(100) with relationship" do
|
125
|
+
Exhibit.feel Exhibit.limit(100).includes(:user)
|
126
|
+
end
|
127
|
+
|
128
|
+
x.report "Model.all limit(10,000)" do
|
129
|
+
Exhibit.look Exhibit.limit(10000)
|
130
|
+
end
|
131
|
+
|
132
|
+
x.report 'Model.named_scope' do
|
133
|
+
Exhibit.limit(10).with_name.with_notes
|
134
|
+
end
|
135
|
+
|
136
|
+
x.report 'Model.create' do
|
137
|
+
Exhibit.create(exhibit)
|
138
|
+
end
|
139
|
+
|
140
|
+
x.report 'Resource#attributes=' do
|
141
|
+
e = Exhibit.new(attrs_first)
|
142
|
+
e.attributes = attrs_second
|
143
|
+
end
|
144
|
+
|
145
|
+
x.report 'Resource#update' do
|
146
|
+
Exhibit.first.update(name: 'bob')
|
147
|
+
end
|
148
|
+
|
149
|
+
x.report 'Resource#destroy' do
|
150
|
+
Exhibit.first.destroy
|
151
|
+
end
|
152
|
+
|
153
|
+
x.report 'Model.transaction' do
|
154
|
+
Exhibit.transaction { Exhibit.new }
|
155
|
+
end
|
156
|
+
|
157
|
+
x.report 'Model.find(id)' do
|
158
|
+
User.find(1)
|
159
|
+
end
|
160
|
+
|
161
|
+
x.report 'Model.find_by_sql' do
|
162
|
+
Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
|
163
|
+
end
|
164
|
+
|
165
|
+
x.report "Model.log" do
|
166
|
+
Exhibit.connection.send(:log, "hello", "world") {}
|
167
|
+
end
|
168
|
+
|
169
|
+
x.report "AR.execute(query)" do
|
170
|
+
ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}")
|
171
|
+
end
|
172
|
+
end
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
class Person < ActiveRecord::Base
|
5
|
+
establish_connection :adapter => 'sqlite3', :database => 'foobar.db'
|
6
|
+
connection.create_table table_name, :force => true do |t|
|
7
|
+
t.string :name
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
bob = Person.create!(:name => 'bob')
|
12
|
+
puts Person.all.inspect
|
13
|
+
bob.destroy
|
14
|
+
puts Person.all.inspect
|