【发布时间】:2018-12-14 04:48:19
【问题描述】:
我有一个 Rails 应用程序,我正在尝试使用在文本字段中找到 @ mentions 的模型文件,然后我希望它使用 after_create 回调通知 @mentioned 方
class Post < ApplicationRecord
after_create :notifiy_users
def notifiy_users
mentioned_users.each do |user|
Notification.create!(recipient: user,
actor: self.user,
action: 'mentioned',
notifiable: self)
end
end
def mentions
@mentions ||= begin
regex = /@([\w]+)/
matches = body.scan(regex).flatten
end
end
def mentioned_users
@mentioned_users ||= User.where(username: mentions)
end
end
在local_env 上,这可以工作并且通知已创建并保存,但是当我推送到生产环境时,这就像从未调用过after_create 一样,并且我没有从 notify_users 方法中得到任何回报。
任何帮助或建议以更好的方式处理此问题将不胜感激。
【问题讨论】:
-
在生产中,你的
mentioned_users很可能是空的。 -
你想从模型哪里调用控制器动作??
-
从模型中调用控制器动作听起来是个坏主意。你为什么要这样做?
标签: ruby-on-rails ruby model-view-controller