-4

I need to call current_user on a model (I have installed devise). I use this method but it doesn't work:

class Todo
  def category_name
    category.try(:name)
  end

  def category_name=(name)
    self.category = Category.find_or_create_by(name: name, user_id: current_user)
  end

end

how can I fix it?

Turk
  • 232
  • 3
  • 16

1 Answers1

0

current_user is a devise helper to be used on Views and Controllers. If you have an instance method that needs the current_user you should probably move it's logic to a Controller.

It's better explained here: Access to current_user from within a model in Ruby on Rails

Community
  • 1
  • 1
Lucas Moulin
  • 2,450
  • 3
  • 32
  • 41