为什么我不想在任何地方使用inverse_of?

时间:2013-02-18 00:49:41

标签: ruby-on-rails ruby associations rails-activerecord

如下所述:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

inverse_of似乎告诉Rails缓存内存关联并最小化数据库查询。他们的例子是:

 class Dungeon < ActiveRecord::Base
   has_many :traps, :inverse_of => :dungeon
   has_one :evil_wizard, :inverse_of => :dungeon
 end

 class Trap < ActiveRecord::Base
   belongs_to :dungeon, :inverse_of => :traps
 end

他们立即跟随:

 for `belongs_to` associations `has_many` inverse associations are ignored.

所以我有几个问题。

  1. has_many的{​​{1}}是否忽略了反对关联?如果是这样,他们的榜样有意义吗?不应该只是不做任何事情吗?
  2. 据我所知(假设它做了什么)所有这些允许做的事情是这样的:

    belongs_to

    最后调用dungeon.traps.first.dungeon 不生成一个全新的查询,而只是达到内存关联。假设这是正确的,为什么我不想要这种行为?为什么我不会在每个协会上贴上.dungeon

2 个答案:

答案 0 :(得分:6)

我开始撰写有关rails inflector的内容,以及当关联不是模型的直接变形时,如何使用inverse_of指示它是什么。但后来我滚动到你提到的部分,这就是我看到它的方式。说你有类似的东西:

# let's pick a dungeon
d = Dungeon.first

# say you find also find a trap that belongs to this particular d
t = Trap.find(...)

# then t.dungeon is the exact same object as d
d == t.dungeon

当然dungeon.traps.first.dungeon并没有真正意义,我怀疑这就是为什么存在的原因。我个人不知道我会在哪里以及如何使用它,但他们给出的例子似乎填补了一个用例。它是这样的:

# you have an attribute level on dungeon
d.level # => 5

# now say you have a comparison after a modification to level
d.level = 10

# now without inverse_of the following thing occurs
d.level         # => 10
t.dungeon.level # => 5

# d was updated and since t.dungeon is a whole different object 
# it doesn't pick up the change and is out of sync but using invers_of you get
d.level         # => 10
t.dungeon.level # => 10

# because d and t.dungeon are the very same object

希望澄清事情。

答案 1 :(得分:4)

好消息!在Rails 4.1中,基本关联*将自动设置inverse_of

*更多便利意味着更多边缘案例...... 自动inverse_of 适用于 指定以下任何选项的关联:

  • :through
  • :foreign_key
  • :conditions
  • :polymorphic

资源:

http://edgeguides.rubyonrails.org/4_1_release_notes.html http://wangjohn.github.io/activerecord/rails/associations/2013/08/14/automatic-inverse-of.html