Railsの英語のドキュメントを自分なりに解釈してみた。自分が確認したい、ネスト構造・1対1関係について翻訳。
出典
Ruby on Rails 6.0.3.1
Module
ActiveRecord::NestedAttributes::ClassMethods
activerecord/lib/active_record/nested_attributes.rb
Active Record Nested Attributes
Nested attributes allow you to save attributes on associated records through the parent.
→ネストされた属性(:nameとか:created_atとかそーいう類)を使用すると、親レコードを介して関連づけられた属性を保存できます。
Cf.ネストとは入れ子構造とも呼ばれ、ある記述の中に入れ子構造で別の記述をする方法です。
ルーティングでいうと、あるコントローラへのルーティングの記述の中に、別のコントローラへのルーティングを記述するということを指します。
By default nested attribute updating is turned off and you can enable it using the
accepts_nested_attributes_for
class method.
→デフォルトでは、ネストされた属性は更新されず、クラスメソッドである、accepts_nested_attributes_for
を使うことで更新できます。
When you enable nested attributes an attribute writer is defined on the model.
→ネストされた属性を有効にすると、attribute writerがモデルで定義されます。
The attribute writer is named after the association, which means that in the following example, two new methods are added to your model:
→attribute writerは、アソシエーション後に命名されます。つまり、次の例では2つの新しいメソッドがモデルに追加されます。
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end
追加されるメソッド
1.author_attributes=(attributes) 2.pages_attributes=(attributes)
Note that the :autosave option is automatically enabled on every association that accepts_nested_attributes_for is used for.
→ただし、autosaveオプションは、accepts_nested_attributes_for
が使われるすべてのアソシエーションで自動的に有効になることをご注意ください。
1対1関係の場合(One-to-one)
Consider a Member model that has one Avatar:
→Avatar属性を持つ、Memberモデルの例
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar
end
Enabling nested attributes on a one-to-one association allows you to create the member and avatar in one go:
→1対1の関連付けでネストされた属性を有効にすると、メンバーとアバターを一度に作成できます。
params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } }
member = Member.create(params[:member])
member.avatar.id # => 2
member.avatar.icon # => 'smiling'
It also allows you to update the avatar through the member:
→また、memberを通して属性avatarを更新することもできます。
If you want to update the current avatar without providing the id, you must add :update_only option.
→IDを指定せずに現在の属性avatarを更新する場合、:update_onlyオプションを追加する必要があります。
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar, update_only: true
end
params = { member: { avatar_attributes: { icon: 'sad' } } }
member.update params[:member]
member.avatar.id # => 2
member.avatar.icon # => 'sad'
By default you will only be able to set and update attributes on the associated model. If you want to destroy the associated model through the attributes hash, you have to enable it first using the :allow_destroy option.
→デフォルトでは、関連付けられたモデルの属性のみを設定および更新できます。属性ハッシュを使用してアソシエーションモデルを破棄する場合は、最初に:allow_destroy
オプションを使用してモデルを有効にする必要があります。
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar, allow_destroy: true
end
Now, when you add the _destroy key to the attributes hash, with a value that evaluates to true, you will destroy the associated model:
→ここで、_destroyキーを属性ハッシュに追加し、値がtrueと評価されると、関連するモデルが破棄されます。
member.avatar_attributes = { id: '2', _destroy: '1' }
member.avatar.marked_for_destruction? # => true
member.save
member.reload.avatar # => nil
Note that the model will not be destroyed until the parent is saved.
Also note that the model will not be destroyed unless you also specify its id in the updated hash.
→親モデルが保存されるまで、子モデルは破棄されないことに注意してください。 また、更新されたハッシュでそのIDを指定しない限り、モデルは破棄されないことにも注意してください。
多対多は、時間ある時に。
参照
RailsAPI(ActiveRecord::NestedAttributes::ClassMethods)
https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
ネストとアソシエーション
https://qiita.com/chopesu_se/items/c7362380865cf978b158