Comparing ruby DateTime with sqlite Date - ruby

I am attempting to pull tweets from twitter and store them in an sqlite db. In order to prevent duplication I wish to ignore tweets where the user AND the time created already exist in the DB. So my code is
if Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', (tweet_results.created_at), tweet_results.from_user_id_str])
Now, through taking out the user_id parts i can tell that the problem is with the comparison between
twitter_created_at
tweet_results.created_at
I have been in the debugger and seen that the dates do match but i cannot get the statement to come back true.
thanks in advance.

Related

How to query between two datetimes using Laravel and Eloquent but but datetimes are not ordered? (error Prepared statement contains too many place...)

Background:
Data is retrieved every minute from an API and stored in a 'sensors' table.
If the data is older than one year. They will be summarized, saved as one new entry in the database and the old data will be deleted. Therefore the column 'date' is not sorted. An update to a specific date is not possible, since I cannot be sure whether the sensor actually generated data at that time.
Now to my problem:
I have a querie that looks something like this
private function getDataFromPeriod()
{
return Sensor::whereBetween('date', [$this->start, $this->end])->orderBy('date')->get();
}
Where $start and $end are in this format for example: '2022-12-31 01:59:59'.
This function is called during the function render() and that works (at the beginning the data from the last year are sorted)). However, when I call this function repeatedly in a different place, the error 1390 Prepared statement contains too many placeholders comes up.
I suspect this is because the query is not good.
Does anyone have an idea how I can improve the query or where else the error could be?
Thanks for your help
you don't need to sort by date;
just replace your code with this:
return Sensor::where('date','>=', $this->start)->where('date','<=', $this->end)->get();
this line will give you the sensors between your start and end date

whereBetween dates trouble for getting results in Eloquent Laravel

I have some trouble with getting the correct output of a Eloquent Query.
$checkIn = date(request()->checkin);
$checkOut = date(request()->checkout);
$bookedRooms = App\Booking::whereBetween('checkin', [$checkin, $checkout])->whereBetween('checkout', [$checkin, $checkout])->pluck('room_id');
$availabileRooms = App\Room::whereNotIn('id', $bookedRooms->toArray())->sum('number_of_rooms');
What I have is a checkin and checkout date.
It is a database for a Hotel.
The Hotel has Rooms. There a different types of Rooms, and each Room can exist multiple times. That is set in the Room-table with an integer value of the rooms available in column number_of_rooms
What I want, it the output of the percentage that is still available of a specific Room and the total availability of Rooms in the date range.
I thought that it can be achieved with the code given before, but that can be not.
Can someone help me out? Probably it's simpler then I can think of now..
i looked your code, but could you please tell me one thing. what is the date format of checkin and checkout you getting.
make sure that you getting date format is like 2019-07-04 (YYYY-mm-dd).
i hope this will help you.

only show events who are greater than or equal laravel

Im working in a project where I display different events in my view. The goal for now is to only show events that are upcoming, so it doesn't show events that are older than today.
The users of my page can create an event and set a sertain date for it.
The data is stored into my db like this:
$table->string('datum');
In the view it return it like this:
06.03.2019
My controller to return the data looks like this:
$date = date('d.m.Y');
$evententries = Event::where('datum', '>=', $date)->orderBy('datum', 'asc')->take(3)->get();
but it's somehow not working..
Anyone got any Ideas of how to fix this and what my issue is here?
btw, I'm using laravel 5.7.
Thank you :)
Hmm you are doing it on wrong way from begin...
First: why you are using string to store date value? there is much better options to store date within database...
Second: you are using date format which cannot be compared on that way (using < > =)
when you are comparing strings ie 08.03.2019 will be always smaller than 10.01.2016 because 1 > 0... so if you are comparing dates use:
db format for storing dates
for comparing use format yyyy-mm-dd because on that way even simple string comparison will give you correct result...

How do I add a date column and integer column together using ActiveRecord?

I have two columns in a database once called purchase_date and the other called lifetime. I am trying to filter the results so that only the assets that are not passed their lifetime are shown when given the current tax year.
I've tried
Asset.where("? < DATE_ADD(purchase_date,INTERVAL lifetime YEAR) AND purchase_date >= ?",Date.new(2012,1,1),Date.new(2012,1,1))
But, then I realized that this would be tied to MySQL if it did work, which is fine if this was just going to work with my full aplication. But, I want something that is database agnostic especially since this is being tested against Sqlite. Sqlite with this query gives me the following exception:
ActiveRecord::StatementInvalid: SQLite3::SQLException: near lifetime
How can I add an integer column to a date column in my sql statement in an database agnostic way?
Update
I have played around with Squeel, but I don't think its going to work. It may be possible to detect the adapter and base it off of that.
Why not add an :expires_at field? You can set a :before_save hook to make sure it's automatically kept in sync with your :lifetime field.
before_save :set_expiration_date
scope :not_expired, where("expires_at > ?", DateTime.now)
def set_expiration_date
return unless purchase_date.changed? || lifetime.changed?
self.expires_at = purchase_date + lifetime.years
end

how to chop a DataMapper Collection into one Collection per day?

I have a DataMapper::Collection Object. Each of it's entries has a created_at property. I want to render the entries into html tables, one table per day (I use Sinatra for that).
It was no problem to render everything into one table, but I didn't get it to do so for every day. I thought of an array of DataMapper::Collection objects over which I would just iterate and do the job. But I don't know how to build such an array :/
Does anyone know how to solve my problem, or does anyone have a different/better approach?
Thanks in advance!
You have (at least) two options. The first is to let the database do the work for you. I don't know about datamapper but most database mappers (!) have functionality to group using SQL's GROUP BY. In this case you would have to use a database function to get the date out of the timestamp and then group on that. This is the fastest option and if you and future maintainers are familiar with relational databases probably also the best.
The second option is to to the mapping in your code. I can't come up with an elegant Ruby thing right now but you could at least do:
mapped_result = Hash.new [] # initiates each new entry with empty array
mapper_collection.each do |one_record|
mapped_result[one_record.created_at.strftime '%Y-%m-%d'] << one_record
end
and then you can get to record for a day with
mapped_result['2012-11-19']

Resources