We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 711eabe commit 0f45187Copy full SHA for 0f45187
db/news.yml
@@ -0,0 +1,2 @@
1
+---
2
+news: []
lib/tasks/import_news.rake
@@ -0,0 +1,24 @@
+require 'yaml'
+
3
+namespace :news do
4
+ desc "db/news.yml を読み込んで News テーブルを upsert する"
5
+ task import_from_yaml: :environment do
6
+ yaml_path = Rails.root.join('db', 'news.yml')
7
+ raw = YAML.load_file(yaml_path)
8
9
+ # entries を計算
10
+ entries = raw['news'] || []
11
12
+ entries.each do |attrs|
13
+ news = News.find_or_initialize_by(url: attrs['url'])
14
+ news.assign_attributes(
15
+ title: attrs['title'],
16
+ published_at: attrs['published_at']
17
+ )
18
+ news.save!
19
+ puts "[news] #{news.published_at.to_date} #{news.title}"
20
+ end
21
22
+ puts "Imported #{entries.size} items."
23
24
+end
0 commit comments