如果条件不成立,我想创建一个 before_filter 将用户重定向到其他页面。
我有四个模型:
class Player < ActiveRecord::Base
has_many :matches, through: :inscriptions
has_many :inscriptions
class Inscription < ActiveRecord::Base
belongs_to :match
belongs_to :player
class Match < ActiveRecord::Base
has_many :players, through: :inscriptions
has_many :inscriptions
class Stat < ActiveRecord::Base
belongs_to :player
每个人都player
可以match
通过创建一个inscription
. 然后,每个人都player
可以像他们一起打过一场比赛一样多次记录其他人,但不能更多。
我想防止 aplayer
能够注意到其他玩家的次数超过他们一起玩的次数。
所以我有 :
def is_noteable?(player)
@matchs_du_joueur = Inscription.where(:player_id => current_user.id)
#all the `inscriptions` of a `match` from current_user
@joueur = Player.joins(:inscriptions).where(:inscriptions => { :match_id => @matchs_du_joueur.map(&:match_id)}).group(:player_id).where('player_id <> ?', current_user.id)
#all the `players` that have played with `current_user`
@combien_de_truc = Inscription.where(:match_id => @matchs_du_joueur.map(&:match_id), :player_id => @player.id).count
#to know how many `inscriptions` they have in a common `match`
@combien_de_notes = Stat.where(:player_id => @player.id, :auteur => current_user.id).count
#to know how many times `current_user` has noted the player
@combien_de_notes <= @combien_de_truc
#if `current_user` has noted `player` less time than he played with him
end
但这并不能如我所愿......似乎没有限制。
我希望你能理解我。谢谢你读我。