Terminal
rails g model notification user:references task:references action:integer checked:boolean
db/migrate
class CreateNotifications < ActiveRecord::Migration[6.0]
def change
create_table :notifications do |t|
t.references :task, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.integer :action
t.boolean :checked, default: false, null: false
t.timestamps
end
end
end
Terminal
rails db:migrate
1 to 1
because the notification is issued when the task deadline is approaching or has passed.app/models/user.rb
class User < ApplicationRecord
has_many :notifications, dependent: :destroy
end
app/models/task.rb
class Task < ApplicationRecord
belongs_to :user
has_one :notification, dependent: :destroy
end
app/models/ntification.rb
class Notification < ApplicationRecord
default_scope->{order(created_at: :desc)}
belongs_to :task
belongs_to :user
enum action: { warning: 1, expired: 2}
end
enum column name: {Name definition:Numerical value}
It is an enumeration type, and when actually registering, save the name definition.
python
rails g controller notifications index
config/routes.rb
Rails.application.routes.draw do
resources :notifications,only: [:index]
end
app/controllers/notifications_controller.rb
class NotificationsController < ApplicationController
def index
@notifications = current_user.notifications
@notifications.where(checked: false).each do |notification|
notification.update(checked: true)
end
end
end
ruby:app/views/layouts/_header.html.slim
- if unchecked_notifications.any?
p.text-danger ★
= link_to "notification",notifications_path, class: 'nav-link'
app/helpers/notifications_heler.rb
module NotificationsHelper
def unchecked_notifications
@notifications = current_user.notifications.where(checked: false)
end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include NotificationsHelper
end
What is Rake
Rake is a function that allows you to create code written in Ruby as a task and call and execute it as needed.
Here are some situations where you can use the rake task.
Linkage of some kind of data
Database backup
Update and delete data on a regular basis
As for adding a path to be autoloaded
did not work, I write it directly in the rake file.
python
rails g task notification
rails g task filename
lib/tasks/notification.rake
namespace :task do
desc "notification/Move once a day."
task :notification => :environment do
today = Date.today
tasks = Task.all
n = tasks.count
today = Date.today
tasks.each do |task|
user = task.user
if today + 1 == task.deadline.to_date
Notification.create(task_id: task.id, user_id: user.id, action: "warning")
end
if task.notification.nil? && today > task.deadline.to_date
Notification.create(task_id: task.id, user_id: user.id, action: "expired")
elsif task.notification.present? && today > task.deadline.to_date
task.notification.update(action: "expired")
end
n -= 1
puts "remaining#{n}/#{tasks.count}"
end
end
end
rake tasks:notification
rake [name of namespece]:[task name]
In my case, it's heroku, so it seems to use heroku scheduler
.
I will summarize it again.
gem: whenever
is a gem that has been added / rewritten to crontab.
heroku addons:add scheduler:standard
Set the time and task you want to run on Heroku Scheduler
heroku run rake task:notification
Recommended Posts