RAILS:具有嵌套资源的Before_filter

时间:2013-06-20 21:58:13

标签: ruby-on-rails

如果条件不正确,我想创建一个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可以记录其他人,因为他们一起玩了很多次,但不是更多。

我想阻止player能够比其他人一起玩的次数多注意其他玩家。

所以我有:

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

但这并不像我想要的那样发挥......似乎没有限制。

我希望你能理解我。谢谢你读我。

0 个答案:

没有答案