自协会方法 - 可以这样做 [英] Custom Association Method - Can This Be Done
问题描述
我有三个型号:赌注,赛事,赛程表及WagerType。我创建了一个赌注一个的has_many关联,并增加了一个自定义的关联方法(in_wager)。该方法的目的是滤除每个下注正确种族。 (有些赌注跨越多个种族)。我希望能够做到出头,如:Wager.first.races.in_wager并具有相应的比赛返回。
I have three models: Wager, Race, RaceCard and WagerType. I've created a has_many association in Wagers and have added a custom association method (in_wager). The purposes of the method is to filter the correct races for each wager. (Some wagers span multiple races). I'd like to be able to do somethings like: Wager.first.races.in_wager and have the appropriate races returned.
class Wager < ActiveRecord::Base
belongs_to :wager_type
belongs_to :race_card
has_many :races, :through => :race_card do
def in_wager
where('races.race_nbr BETWEEN ? AND ?', a, b)
end
end
end
我的自定义的方法工作正常,如果我硬$ C C为a和b的值$,但是,我需要这些值是动态的。具体地讲,b的值应该从下注模型等于race_nbr属性:
My custom method works fine if I hardcode the values for a and b, however, I need those values to be dynamic. Specifically, the value of b should equal the race_nbr attribute from the Wager model:
b = wagers.race_nbr
和a的值应等于B减种族为特定下注类型的数量(所知道的腿)加1:
and the value of a should equal b minus the Number Of Race for the particular Wager type (know as Legs) plus 1:
a = b - Legs + 1
有关双腿值在WagerType模型。注赌注belong_to WagerType和WagerType的has_many赌注。因此,可以pssed作为前$ P $:
The value for legs is in the WagerType model. Note Wagers belong_to WagerType and WagerType has_many Wagers. Therefore, a could be expressed as:
a = (b - (select wager_types.legs where wagers_types.id = wagers.wager_type_id) + 1)
我的问题:是否真能与我in_wager关联方法做到这一点。我一直在敲打我的头在这个一对夫妇晚上,现在并不能完全弄清楚如何分配正确的价值观,以a和b。此外,如果你觉得我来了,在这个错误的方式,我会很高兴听到替代方法。感谢您的帮助。
MY Question: Is it actually possible to do this with my in_wager association method. I've been banging my head on this for a couple of evening now and can't quite figure out how to assign the correct values to a and b. Also if you feel I'm coming at this the wrong way, I'd be happy to hear alternative approaches. Thanks for your help.
注:我从来没有真正提到了赛程表或种族模型。它们具有以下关联:
Note: I never really mentioned the RaceCard or Races models. They have the following associations:
class RaceCard < ActiveRecord::Base
has_many :races
end
class Races < ActiveRecord::Base
belongs_to :race_card
has_many :wagers, :through => :race_card
end
更新:我读的设计模式在Ruby中昨晚遇到了PROC。我要看看我能不能用它在该协会的方法来计算值a和b。
Update: I was reading Design Patterns in Ruby last night and came across the Proc. I'm going to see if I can use it within the Association method to calculate the values for a and b.
推荐答案
您可以使用 self.proxy_association.owner
来获取父对象的关联方法里面。从那里,你可以得到你想要的值。
you can use self.proxy_association.owner
to get the parent object inside of an association method. From there you can get the values you want.
如果我正确地理解你的模型,然后在code应该是这个样子。
If I understand your models correctly then the code should look something like this.
class Wager < ActiveRecord::Base
belongs_to :wager_type
belongs_to :race_card
has_many :races, :through => :race_card do
def in_wager
owner = self.proxy_association.owner
b = owner.race_nbr
a = b - owner.wager_type.legs + 1
where('races.race_nbr BETWEEN ? AND ?', a, b)
end
end
end
在我从Rails的API参考了这<一href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Association+extensions">Association扩展(提及 proxy_association
是在部分的底部)。
The I got this from the Rails api reference to Association Extensions (The reference to proxy_association
is at the bottom of the section).
这篇关于自协会方法 - 可以这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!