I have an array of object. Every object have a start_date and and end_date (DATETIME).
I save it with : #arroy_of_objects.each(&:save)
But I can't have two date in the same time. If a start_date is equal and between an another date range, I don't want to save it.
How can I do it with rails ? In a validate in the model or directly in my controller
Related
I have a model Entry with attributes date and time. I want to add filters on index.html.slim with 4 input form:
1) start date
2) start time
3) end date
4) end time
And I want this form to find all records in this range.
You can combine date and time fields to datetime and for search in range:
Entry.where(your_datetime_field: start_datetime..end_datetime)
I want to be able to list any 'events' in my database which occur on the same date as another event being creating in my method.
The column I want to compare is 'starts_at' and is a DateTime.
How can I just compare the date that events occured on instead of both date and time?
I need to add the query to this where call:
events = Event.where(location_id: location.id)
I already have a date object date to compare to, I just don't know how I can get just the date part of the events already in the database?
is it something along the lines of date(starts_at:)?
events = Event.where(location_id: location.id, date(starts_at:): date)
I am using sqlite database.
Check if this works with your DB or not:
events = Event.where(location_id: location.id).where("DATE(starts_at) = ?", date)
OR, compare in datetime as per active record:
events = Event.where(location_id: location.id,starts_at: date.beginning_of_day..date.end_of_day)
Pretty new to Ruby, I am having trouble implementing some logic
I have a lot of dates (effective and expiration dates).
I also have 2 objects. An original object, and a new copied object (it is a copy of the original)
I want to loop through each pair of dates, and check the orignal objects dates fields to see if ti had any. If it did have dates, then I was to:
use the original objects expiration date as the new objects effective date
default the new objects expiration date to the new effective date + 1 year
Here is my code, I get this error:
undefined method `id' for nil:NilClass
CODE:
DATE_FIELDS = [:agency_effective_on, :agency_expire_on,
:field_effective_on, :field_expire_on,
:product_manager_effective_on, :product_manager_expire_on,
:officer_effective_on, :officer_expire_on,
:regional_officer_effective_on, :regional_officer_expire_on]
#copy_to = #copy_from.dup
DATE_FIELDS.each.slice(2) do |field|
if !#copy_from.send(field[0].to_sym).nil? #if the ORIGNAL date is not nil
#copy it to the new Objects dates
#copy_to.send("#{field[0]}=".to_sym, #copy_from.send(field[0]))
#copy_to.send("#{field[1]}=".to_sym, #copy_from.send(field[1]).to_date + 365)
end
end
Ok so I had each.slice instead of each_slice
I'm working on an event system where admins can select events to feature and add promotional text. As they need to add additional text the featured events are in their own table and reference the event details.
I'm now trying to output featured events for each day in the next week. Simplified example:
day = DateTime.now.to_date
featured_events = #featured_events.where('event.start_datetime = ?', day)
The problem is that start_datetime is in datetime format and day is in date format so it always outputs nothing. Is it possible to compare these two values with .where()?
Thanks!
May be you're looking for this:
#featured_events.where('event.start_datetime > ? and event.start_datetime < ?', DateTime.now, 1.day.from_now.beginning_of_day)
You don't say what database you're using.
In PostgreSQL we'd truncate the datetime inside the DB query so the DBM can do the compare. On MySQL we'd use a function to convert the datetime to a date.
Lets say I want to compare the dates only of two datetime columns within 1 record. So I don't want the time looked at.
I.e.
viewed_date, and updated_at (I added viewed_date) are two datetime formats, but I only want to see if they occurred on the same day or days apart. The problem with datetime is that its comparing the times, which is just too specific for me right now.
Thanks
-Elliot
Declare a new attribute that contains just the date:
class WhateverModel < ActiveRecord::Base
...
def viewed_date_only
viewed_date.to_date
end
end
Use that for your comparison in the controller or wherever.
You can compare only date of object without comparing time like this:-
start_date :-"2015-04-06 15:31:43 +0530"
end_date :- "2015-04-16 15:31:43 +0530 "
post_review.all.where("(created_at::date >= :start_date) AND
(created_at::date <= :end_date)", start_date: start_date.strftime("%Y-%m-%d"),end_date: end_date.strftime("%Y-%m-%d"))
Above query gets all the records made between 6 to 16 April