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 ActiveRecord - how to fetch records between two dates
data = ModelName.where("today >= from_date AND today <= to_date")
Ruby on Rails- Query Amount between two dates from console
You can use sum
:
Model.sum(:amount)
To select dates, just add where
:
Model.where(date: from_date..to_date).sum(:amount)
How to find data between two dates in ruby on rails
You have inverted the parameters - it should be
ViewsLog.where(:created_at=>3.days.ago..Time.now)
Dynamically get records between two dates
As you wanted to achieve this from the model point of view,
You can use the dynamic scope of rails to achieve this,
In your model,
Method1:
class Vote < ActiveRecord::Base
scope :average_score, ->(date1,date2) { where(date: (date1)..(date2) )}
end
Now, on your controller or the view , you can call something like,
@average = Vote.average_score(Date.today - 7 , Date.today).average(:id)
Method 2:
You can take a method and call the scope from the method.
class Vote < ActiveRecord::Base
scope :average_score, ->(date1,date2) { where(date: (date1)..(date2) )}
def self.average_score_value(d1, d2)
self.average_score(d1,d2).average(:id)
end
end
Now, on your controller or the view , you can call something like,
@average = Vote.average_score_value(Date.today - 7 , Date.today)
Here is an example with user table, I have taken created_at
column
2.3.1 :036 > User.average_score(Date.today - 7,Date.today).average(:id)
(0.5ms) SELECT AVG(`users`.`id`) FROM `users` WHERE (`users`.`created_at` BETWEEN '2016-10-19' AND '2016-10-26')
=> nil
The scope is,
scope :average_score, ->(date1,date2) { where(created_at: (date1)..(date2) )}
Here is the reference
Finding records between date range
You can simply do
Price.where(:date => start_date..end_date)
This will result in the following SQL
( for start and end dates - '2014-03-27', '2014-03-28')
SELECT `prices`.* FROM `prices` WHERE (`prices`.`date` BETWEEN '2014-03-27' AND '2014-03-28')
EDIT:
Realized that this is the query you are looking for. Thanks, Coenwulf for pointing it out
Price.where(['start_date < ? AND end_date > ?', date, date])
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.
Search records between two dates - Ruby on Rails
Try:
def self.date_search(search_string)
self.where({release_date: Time.now..search_string})
end
This will give you entries where release_date is between the current time and the search_string (inclusive of the search string since you use two dots(..), it would be exclusive of the search string if you used three dots (...)
Related Topics
How to Generate a Permutations or Combinations of N Rows in M Columns
Sql Query to Sum Fields from Different Tables
How to Display the Date as Mm/Dd/Yyyy Hh:Mm Am/Pm Using SQL Server 2008 R2
Get the List of Stored Procedures Created And/Or Modified on a Particular Date
How to Remove SQL Azure Data Sync Objects Manually
Count of Unique Values in a Rolling Date Range for R
What Is Db/Development_Structure.SQL in a Rails Project
Efficiently Duplicate Some Rows in Postgresql Table
How to Retrieve a Column Value by Name Using Golang Database/Sql
Update Statement with Multiple Where Conditions
Oracle SQL: Understanding the Behavior of Sys_Guid() When Present in an Inline View
How to Select The Record with The 2Nd Highest Salary in Database Oracle
Why Is There No "Product()" Aggregate Function in Sql
What Is {Ts '2013-04-02 00:00:00'}
Can You Have an Inner Join Without the on Keyword
How to Select a Fixed Number of Rows for Each Group
Is Count(*) in SQL Server a Constant Time Operation? If Not, Why Not