Rails Activerecord - How to Fetch Records Between Two Dates

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



Leave a reply



Submit