accessing devise current_user within model

后端 未结 4 1611
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 16:03

hi i am trying to access current_user within a model for the purpose of creating an element on the fly with find_or_create_by.

the following is the method within my mode

4条回答
  •  有刺的猬
    2021-01-25 16:37

    current_user is not accessible from within model files in Rails, only controllers, views and helpers.

    What you should do is to pass the current_user.team_id to the opponent_name method like this:

    def opponent_name=(name, current_user_team_id)
      self.opponent = Opponent.find_or_create_by_name_and_team_id(name,current_user.team_id) if name.present?
    end
    

提交回复
热议问题