タグ

2015年4月7日のブックマーク (2件)

  • ActiveRecord::Associations::ClassMethods

    Ruby on Rails 7.1.3.4 Module ActiveRecord::Associations::ClassMethods activerecord/lib/active_record/associations.rb Active Record Associations Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like “Project has one Project Manager” or “Project belongs to a Portfolio”. Each macro adds a number of methods to the class whic

    tsimo
    tsimo 2015/04/07
    “If you are using a belongs_to on the join model, it is a good idea to set the :inverse_of option on the belongs_to, which will mean that the following example works correctly (where tags is a has_many :through association):”
  • ActiveRecord4(Rails4)のリレーションをinverse_ofで最適化する方法

    ActiveRecord4でhas_manyリレーションを定義する際、逆方向のリレーションをinverse_ofで指定することで、オブジェクトをメモリ空間内で効率化することができます。 例えば下記のようなモデルがあるとします。 上記のリレーションにおいて、DungeonのtrapsとTrapのDungeonは逆方向のリレーションであるため、同一のオブジェクトはメモリ内でも同一に扱うことができるはずです。 しかし、ActiveRecord4のデフォルトの動作ではそのような最適化は行ってくれません。 d = Dungeon.first t = d.traps.first d.level == t.dungeon.level # => true d.level = 10 d.level == t.dungeon.level # => false 上記のコードにおいて、dとt.dungeonはDB

    tsimo
    tsimo 2015/04/07