あるWebサービスの一機能として、指定したコンテンツの予約投稿機能のようなものを作成したので、そのやり方とそこで学んだことを記していきます。
まずはテーブルを作成します。
必要なデータはpost_date(投稿したい日時), content_id(コンテンツID), content_title(コンテンツ), content_bobyです。他にもいろいろあるかと思いますが、ひとまずこれで。
rails generate model willpost post_date:date content_id:string content_title:integer contet_body:integer
まずは、モデルから
willpost.rb
class willpost < ActiveRecord::Base
belongs_to :User
attr_accessible :post_date :content_id, :content_title, :content_boby
scope :willpost, -> { where('post_date >= ?', Date.today) }
class << self
def willpost_content
contents = content.willpost
end
end
end
投稿したいコンテンツの日付けがDate.todayと一致したらそのコンテンツのデータを取ってくるようにします。
contensの値をContorollerに渡して、Viewで表示したらDoneです!