To clarify, I am building a search that takes three parameters the month/year/week number. I need to know how to return records based on the week number that is passed. For instance, there will be 5 weeks most likely. When I select all three parameters, I should get records back for 7 days or less. As it stands, I have successfully retrieved records by month/year, but now I am introducing the week number as a parameter. How do you pass the week number to the date.new object?
index.html.erb
<%= form_tag amb_chart_records_path, :method => :get, :class => "ui form" %>
<div class="three fields">
<div class="field">
<%= select_month(@month.present? @month : nil, {:include_blank => "Select Month"...%>
</div>
<div class="field">
<%= select_year(@year.present? ? @year : Date.today, {:include_blank => "Select Year"..%>
</div>
<div class="field">
<%= select_tag :week, options_for_select([["Week 1", 1], ["Week 2", 2], ["Week 3", 3], ["Week 4", 4], ["Week 5", 5]], params[:week]), class: 'ui dropdown' %>
</div>
</div>
<div class="field text-center">
<%= submit_tag("Search", :id => "button", :class => "ui button basic green" %>
</div>
<% end %>
amb_chart_records_controller.rb
def index
if params[:commit] == "Previous" || params[:commit] == "Next"
now = Date.new(params[:current_year].to_i, params[:current_month].to_i)
if params[:commit] == "Previous"
target_date = now - 1.month
else
target_date = now + 1.month
end
elsif params.has_key?(:select)
target_date = Date.new(params[:select][:year].to_i, params[:select][:month].to_i, 1)
week_numb = params[:select][:week].to_i
else
target_date = Date.new(Date.today.year, Date.today.month, 1)
week_numb = 1
end
@year = target_date.year.to_i
@month = target_date.month.to_i
@week_numb = target_date.cweek.to_i
@facilities = Facility.where(is_ambulatory_chart_enabled: true)
@ambulatory_chart_records = @facilities.includes(:amb_chart_records).order(record_date: :desc).where(:record_date => target_date..target_date + 1.month)
end
def week(year, month, week_number)
Date.new(year, month, 7 * week_number - 6)
end
To get the beginning of a week in Ruby use the Date#commercial method:
irb(main):019:0> Date.commercial(2020, 25, 1)
=> Mon, 15 Jun 2020
This assumes that weeks begin on a monday. If you need ISO weeks that start on sunday just shift the date a single day with #change :
irb(main):020:0> Date.commercial(2020, 25, 1).change(day: -1)
=> Tue, 30 Jun 2020
If you want to query based on week numbers one way to do it is by passing a range between the start and end date:
w_start = Date.commercial(year, week_number, 1)
w_end = Date.commercial(year, week_number, 7)
Model.where(some_column: w_start..w_end)
As a less polyglot solution you can also use the date functions in the database to extract the week number from date columns:
# Postgres
User.where('EXTRACT(week from created_at)::int = ?', 26)
.where('EXTRACT(year from created_at)::int = ?', 2020)
# MySQL
User.where('WEEK(created_at) = ?', 26)
.where('YEAR(created_at) = ?', 2020)
This is more useful if you are using grouping or aggregation based on the week.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.