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
CodePudding user response:
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?
CodePudding user response:
Double equals is comparison, single equals to set.
def publicaciones
if self.inicio_publicacion == Date.today
self.published = true
else
self.published = false
end
end
But it can also be written much simpler:
def publicaciones
published = inicio_publicacion.today?
end