関連するオブジェクトを埋め込んで、および/またはを使用できるのに、なぜhas_one
アソシエーション(および追加のテーブル、モデル)を使用する必要があるのですか。常に関連付けられたオブジェクトを結合し、複雑なクエリを必要としない場合。例えば:composed_of
serialize
Dog(color、weight)has_one :head
Head(teeth_count、eye_color)
Dog(color、weight、head_teeth_count、head_eye_color)に置き換えて、「計算された属性」または次のような値オブジェクトを使用できます。
composed_of :head, mapping: [%w(head_teeth_count teeth_count), %w(head_eye_color eye_color)]
テーブルが少なく、シンプルで、コードが明確で、モデルが重くなく、偶発的な余分なクエリがなく、パフォーマンスが向上します。
もう一つの例:
ユーザー(メール)has_many :roles
役割(名前)
User(email、roles)に変換します
serialize :roles, Array
私はこれらの場合に何かを逃しましたか?