LoginSignup
10
8

More than 5 years have passed since last update.

初めてのRuby on rails ~予約投稿機能を実装してみた!~

Last updated at Posted at 2014-08-01

ある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です!

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8