Search code examples
ruby-on-railsruby-on-rails-6

How to schedule Posts in Rails 6


devs i'm trying to schedule some posts in my blog module, its a simple scaffold posts, this is my schema.rb

  create_table "posts", force: :cascade do |t|
    t.string "titulo"
    t.string "descripcion"
    t.datetime "inicio_publicacion"
    t.datetime "fin_publicacion"
    t.boolean "ver_proveedores"
    t.boolean "ver_clientes"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.boolean "published", default: true
  end

I Put that published attribute in my model for a reason, to evaluate if the user when entering the initial publication date is equal to the current one, the value of that field should be true so that once I have that I can put another logic in my view to a tag that says "published" or not "unpublished" depending on that value, but I see that it is not working or maybe I am confused and there is another way to do it.

I also thought about placing an accessor method :published but the values ​​it returns are "nil".

  attr_accessor :published

  before_save :publicaciones

  def publicaciones
    if self.inicio_publicacion == Date.today
       self.published == true
    else
       self.published == false
    end
  end
end

Solution

  • inicio_publicacion has a datetime type. It is literraly "day" and "time".

    If you want to ensure that something happened today, it should be

    inicio_publicacion.to_date == Date.today

    or even easier

    inicio_publicacion.today?