2

In my model I have a method that marks a record as pending by changing its status to 2. After which it calls another method in another controller to create a notification containing details of the record that was changed.

e.g.

class Page < ActiveRecord::Base

  def pend_page
    self.update(status: 2)
    Notification.create_notification("#{link_to self.title, pages_path(:status => 2)} marked as pending", @current_user)
  end

end

However it seems Rails doesn't pass helpers for link_to and the routes to the models... as I get the error: undefined method 'pages_path' for #<Page:0x007fd15c996c88>.

How can I make it so that the link_to and pages_path work?

I'm using Rails 4.2.5.1

edit: here is what create_notification looks like:

class Notification < ActiveRecord::Base

  belongs_to :user

  def self.create_notification(content, user)
    notification = Notification.new
    notification.content = content
    notification.user_id = user.id
    notification.status = 0
    notification.save
  end

end
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • See this post http://stackoverflow.com/a/5456103/2968762, for URL related question. – Abhi Dec 20 '16 at 11:40

1 Answers1

-1

This should go in either a service object or in a PORO (plain old ruby object). A model's concerns should begin and end with database related functionality, anything else is in the wrong place.

askl56
  • 364
  • 1
  • 4