Interaction of model methods rails -
inside "course" model:
one method supposed select user_ids belong course
def participants course.joins(:click).pluck(:user_id) end the other method supposed pick random user_id
def set_winner course.participants.sample end however, next error:
undefined method `participants' #<class:0x007fc639811468> if explain me, why doesn't work, i'd grateful.
your illustration doesn't work because define instance methods. , seek run them on class if class methods.to prepare write:
def self.participants course.joins(:click).pluck(:user_id) end def self.set_winner course.participants.sample end or, improve
class course of study < activerecord::base scope :participants, -> { joins(:click).pluck(:user_id) } scope :set_winner, -> { participants.sample } end ruby-on-rails ruby-on-rails-4 model
No comments:
Post a Comment