Rails: :inverse_of and Association extensions

浪尽此生 提交于 2019-12-08 17:15:48

问题


I have the following set-up

class Player < ActiveRecord::Base
  has_many :cards, :inverse_of => :player do
    def in_hand
      find_all_by_location('hand')
    end
  end
end

class Card < ActiveRecord::Base
  belongs_to :player, :inverse_of => :cards
end

This means the following works:

p = Player.find(:first)
c = p.cards[0]
p.score # => 2
c.player.score # => 2
p.score += 1
c.player.score # => 3
c.player.score += 2
p.score # => 5

But the following doesn't behave the same way:

p = Player.find(:first)
c = p.cards.in_hand[0]
p.score # => 2
c.player.score # => 2
p.score += 1
c.player.score # => 2
c.player.score += 2
p.score # => 3

d = p.cards.in_hand[1]
d.player.score # => 2

How can I make the :inverse_of relationship extend to the extension methods? (Is this just a bug?)


回答1:


It does not work because the "in_hand" method has a query that goes back to the database.

Because of the inverse_of option, the working code knows how to use the objects that are already in memory.

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




回答2:


I have found a workaround if (as I am) you're willing to give up the SQL optimisation granted by Arel and just do it all in Ruby.

class Player < ActiveRecord::Base
  has_many :cards, :inverse_of => :player do
    def in_hand
      select {|c| c.location == 'hand'}
    end
  end
end

class Card < ActiveRecord::Base
  belongs_to :player, :inverse_of => :cards
end

By writing the extension to filter in Ruby the full results of the association, rather than narrowing down the SQL query, results returned by the extension behave correctly with :inverse_of:

p = Player.find(:first)
c = p.cards[0]
p.score # => 2
c.player.score # => 2
p.score += 1
c.player.score # => 3
c.player.score += 2
p.score # => 5

d = p.cards.in_hand[0]
d.player.score # => 5
d.player.score += 3
c.player.score # => 8


来源:https://stackoverflow.com/questions/4146278/rails-inverse-of-and-association-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!