【发布时间】:2012-02-01 19:34:15
【问题描述】:
在我的验证中调用我的关联生成的方法时遇到问题。
我的代码很简单:
class Match < ActiveRecord::Base
# Associations
belongs_to :tournament
has_many :match_player_relations
has_many :waiting_players, through: :match_player_relations
has_many :replays
# Validations
validates :tournament_id, presence: true
validates :winner_id, inclusion: { in: waiting_players.map { |wp| wp.id } }
end
我在测试中验证了有一个 waiting_players 方法,它可以正常工作。但是,当我尝试在验证中调用它时,出现以下错误:
/Users/max/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/base.rb:1088:in `method_missing': undefined local variable or method `waiting_players' for #<Class:0x007fc3b498c9c8> (NameError)
from /Users/max/workplace/CloudLeagues/app/models/match.rb:11:in `<class:Match>'
有没有办法解决这个问题?还是我需要删除验证?
【问题讨论】:
-
它可以与
self.waiting_players一起使用吗? -
可能不行,看这个问题:stackoverflow.com/questions/5034988/…
标签: ruby-on-rails ruby-on-rails-3