Rails Activerecord Query Date Range

Rails ActiveRecord Query Date Range

Turns out my controller somehow wasn't set up correctly and was no longer saving the current_user's ID when creating new assignments, which is why they weren't being found. I figured this out using the rails console and running a find on the last few assignments submitted. The user_id was set to nil. Thanks for the assist @mu is too short.

Rails ActiveRecord date between

Just a note that the currently accepted answer is deprecated in Rails 3. You should do this instead:

Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day)

Or, if you want to or have to use pure string conditions, you can do:

Comment.where('created_at BETWEEN ? AND ?', @selected_date.beginning_of_day, @selected_date.end_of_day)

Rails 5 active record query where date range overlaps with other date range

Maybe not as succinct, but I have to do this a lot in a current project and my method is...

start_date = Date.today.at_beginning_of_week
end_date = Date.today.at_end_of_week

@plantings = Planting.where('planting_date_end >= ? AND planting_date_begin <= ?', start_date, end_date)

This covers all overlaps.. if planting starts before the range and ends after the range, if planting starts during the range, if planting ends during the range.

Rails | SQL where date range but inclusive

Use this code:

@availabilities = Availabilities.where("date >= ? AND date <= ?",start_day,last_day)

Passing date range parameters to SQL/ActiveRecord query in Rails

You can simply achieve this by defining a scope like this:

class OrderReport < ActiveRecord::Base
scope :during, -> (start, end) {
where('event_at >= ? AND event_at <=?', start, end)
}
end

Then you can do your query super easy:

OrderReport.during(start_date, end_date).joins(pla pla pla)

Active Record query to find parents with child within a date range and child prior to that date range

You can do this in following ways,

Simple way, get the ids for parent from the first query and apply the filter on parent id in second query,

 q1_pids = Parent.joins(:children)
.where("children.created_at BETWEEN ? AND ?", start, end)
.ids
count = Parent.joins(:children)
.where("children.created_at < ?", start)
.where(id: q1_pids)
.count("parents.id")

Using INTERSECT

q1 = Parent.joins(:children)
.where("children.created_at BETWEEN ? AND ?", start, end)

q2 = Parent.joins(:children)
.where("children.created_at < ?", start)

count = (q1 & q1).count

Second solution just uses array intersection, however you can prepare the SQL statement yourself to fire a single SQL query to the database.

in rails i need to get all records based on a date range, ignoring the year

I would use to_char to transform the dates into strings including just the month and the day. For example 2021-12-31 could be translated into "12-31". That string can then be compared to the range of date strings you are interested in.

users.where("to_char(hire_date, 'MM-DD') BETWEEN '01-01' AND '02-29'")

Note that this, in theory, this would also match invalid date strings like '01-40'. But I guess it is safe to assume that to_char with a valid date will never return such invalid date strings.

Querying for date range in rails

I was able to compensate for UTC by rewriting my scope as follows:

scope :today, where("transfer_date BETWEEN ? AND ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)


Related Topics



Leave a reply



Submit