How to get a Date from date_select or select_date in Rails?
Using date_select gives you 3 separate key/value pairs for the day, month, and year respectively. So you can pass them into Date.new
as parameters to create a new Date object.
An example date_select returned params for an Event
model:
"event"=>
{"name"=>"Birthday",
"date(1i)"=>"2012",
"date(2i)"=>"11",
"date(3i)"=>"28"},
Then to create the new Date
object:
event = params[:event]
date = Date.new event["date(1i)"].to_i, event["date(2i)"].to_i, event["date(3i)"].to_i
You may instead decide to wrap this logic in a method:
def flatten_date_array hash
%w(1 2 3).map { |e| hash["date(#{e}i)"].to_i }
end
And then call it as date = Date.new *flatten_date_array params[:event]
. But this is not logic that truly belongs in a controller, so you may decide to move it elsewhere. You could even extend this onto the Date
class, and call it as date = Date.new_from_hash params[:event]
.
Ruby on Rails Select with date range
range = (DateTime.yesterday..DateTime.now)
User.all.select { |user| range.cover?(user.created_at) }
Rails ActiveRecord - Select all objects by date, using only day, month, year, but ignoring the rest of the date's components
You can use Date#beginning_of_day
and Date.end_of_day
both provided by ActiveSupport
core extensions.
d = '2019-01-20'.to_date
Event.where(created_at:(d.beginning_of_day..d.end_of_day))
This will not necessarily compare "...the date by day, month and year." but instead it will create a between clause that encompasses the entire day. If you want to use the Hash
finder syntax this is probably the best way to go about it.
You could go with
Event.where(
Arel::Nodes::NamedFunction.new('CAST',
[Event.arel_table[:created_at].as('DATE')]
).eq(d)
)
If you really only want to compare the day, month and year.
Option 1 will result in
events.created_at BETWEEN '2019-01-20 00:00:00' AND '2019-01-20 23:59:59'
Option 2 will result in
CAST(events.created_at AS DATE) = '2019-01-20'
Get Rails Date Select Form Helper to display on a single line
Have you tried
float: left;
width: 150px;
The width has to be fixed with float left to make elements "follow" each other.
rails: select record if current date is between two date and show in view
data._mess_price
is an array, so you either have to return the first element in the array:
var messPrice = data._mess_price[0];
or make sure that data._mess_price
is not an array by using first
at the end of your ActiveRecord call:
@mess_current_price = MessPrice.where('mess_id = ? AND DATE(?) BETWEEN start_date AND end_date', @mess.mess_id, @today).first
There is nothing wrong with the way you're using BETWEEN
. The query works fine.
I want a date select box with blank values selected by default
Far easier to do this:
<%= f.date_select :featured_at, {:include_blank => true, :default => nil} %>
Rails - How to select the last date in a table?
You have to order by the date and select a single record:
<%= Reklamer.where(name: 'Lars').order('dato DESC').first %>
You can accomplish this by limiting to a single record as well:
Reklamer.where(name: 'Lars').order('dato DESC').limit(1)
If you just want the last date from the last entry, you can do this:
Reklamer.where(name: 'Lars').last.dato
Related Topics
Best Practices For Reusing Code Between Controllers in Ruby on Rails
Serving Static Files With Sinatra
Why Are My Basic Heroku Apps Taking Two Seconds to Load
Unresolved Specs During Gem::Specification.Reset:
Pass a Variable into a Partial, Rails 3
Convert To/From Datetime and Time in Ruby
How to Switch to Ruby 1.9.3 Installed Using Homebrew
How to "Activate" a Different Version of a Particular Gem
Why Can't I Install Rails on Lion Using Rvm
How to Call a Controller'S Method from a View (As We Call from Helper Ideally)
Why Are Symbols in Ruby Not Thought of as a Type of Variable
How to Use Define_Method to Create Class Methods
Rails 3.1 Limit User Created Objects
How to Monitor Delayed_Job With Monit