LoginSignup
22
20

More than 5 years have passed since last update.

ActiveRecord::Base API を読む際に参考にしたページ

Posted at

環境
- Rails 4.1.1

Ruby on Rails API の ActiveRecord::Base と include しているモジュールの説明を読んだ際のメモ書きと参考にしたページのまとめです。

ActiveRecord::Base

目次代わり
ActiveRecord::Base > Included Modules

テーブル名のマッピングルール
README_rdoc – Object-relational mapping put on rails

ActiveRecord::Core

configurations

console
> ActiveRecord::Base.configurations

ActiveRecord::Persistence

becomes

This is mostly useful in relation to single-table inheritance structures where you want a subclass to appear as the superclass.

touch!
touch, touch, updated_at に touch

親子関係のあるモデルで、子モデルを更新した時に親モデルのupdated_atも更新したくなった。

ActiveRecord::NoTouching

no_touching
Ruby on Rails 4.1 Release Notes #6(Active Record)

touchを無効化する ActiveRecord::Base.no_touching が追加されました。

update
Railsでカラムの値を更新するのは "update_attribute", "update_attributes", それとも "update_attribut

ActiveRecord::ReadonlyAttributes

ActiveRecord::ModelSchema

console
> Post.column_names
=> ["id", "title", "body", "created_at", "updated_at"]

ActiveRecord::Inheritance

initialize_dup
ActiveRecordインスタンスをコピーして新しいレコードを作成する方法

ActiveRecordインスタンスをコピーして、新しいレコードを作成する方法に悩んだが、ActiveRecord::Baseクラスのインスタンスメソッドにそれ専用のメソッドが用意されていた。

ActiveRecord::Scoping

ScopeRegistry
ActiveSupport::PerThreadRegistry

Rails 4 で追加されたモジュール。Threadごとの固有の自分自身インスタンスを自動的に作成し、クラスメソッドのように自分自身にアクセスできます。利用には extend するだけです。

ActiveRecord::Sanitization

ActiveRecord::AttributeAssignment

ActiveModel::Conversion

ActiveRecord::Integration

ActiveRecord::Validations

ActiveRecord::CounterCache

ActiveRecord::Locking::Optimistic

ActiveRecord::Locking::Pessimistic

Railsで悲観的ロックできないの?

という質問がありました。ActiveRecordで楽観的ロックは、ロックをかけたいModelに、lock_version という列を追加すればできます。

ActiveRecord::AttributeMethods

ActiveRecord::Callbacks

ActiveRecord::Timestamp

ActiveRecord::Associations

実用ポリモーフィック関連(基礎編)

・・・ポリモーフィック関連について、どういったものなのか、どんな事に使えるのか、事例を交えて解説してみようと思います。

inverse_of
ActiveRecordの変更点

ActiveRecordの変更点 > inverse_ofその1 > ActiveRecordにはinverse_ofという機能があります。

ActiveModel::SecurePassword

これだけ ActiveModel のモジュール

ActiveRecord::AutosaveAssociation

ActiveRecord::NestedAttributes

ActiveRecord::Aggregations

composed_of

Rails.composed_of

調べ物をしていたら、複数カラムをバリューオブジェクトに構成することで仮想的に一つのカラムとして扱う、composed_ofなるものを見つけたので備忘録します。

7 Patterns to Refactor Fat ActiveRecord Models

  1. Extract Value Objects ... In Rails, Value Objects are great when you have an attribute or small group of attributes that have logic associated with them.

ActiveRecord::Transactions

after_commit

Note that transactional fixtures do not play well with this feature. Please use the test_after_commit gem to have these hooks fired in tests.

ActiveRecord::FixtureSet > Transactional Fixtures

Test cases can use begin+rollback to isolate their changes to the database instead of having to delete+insert for every test case.

ActiveRecord::Reflection

reflect_on_all_associations
モデル間のアソシエーションの情報を取得する方法

belongs_to や has_many で宣言した、 モデル間のアソシエーションの情報を取得するには、 ActionRecord::Reflection の reflect_on_all_associations メソッドを使えばいい。

ActiveRecord::AttributeMethods::Serialization

ActiveRecord::Store

--

Arel

(参考)ActiveRecordを支える技術 - Arelとは何者なのか? (全5回) その1

Rails3, ActiveRecordからは、内部でArelと呼ばれるSQL生成用のライブラリが利用されています。 今回、Arelが内部でどのようにSQLを生成しているのかを調査したので、当ブログにて公開いたします。

22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20