How to list details according to date - ruby-on-rails

I have a properties tables with fields name,price, start_date, end_date.
i want to list properties between start_date and end_date.
I wrote query like this, but i am not getting correct output.
I want to convert date format like 02/12/2015 to 2015/12/02(as it is in database format).
#properties = Property.where("properties.start_date >= ? AND properties.end_date <= ? ",params[:start_date], params[:end_date]).order(updated_at: :desc)
How to list by date?
Any help is appreciatable

You should parse dates first to get them in correct format. Try something like this:
start_date = Date.parse(params[:start_date])
end_date = Date.parse(params[:end_date])
#properties = Property.where("properties.start_date >= ? AND properties.end_date <= ? ",start_date, end_date).order(updated_at: :desc)

Related

How to find all records from the db, where all values of the json field are greater than the current date?

p = Post.last
p.change_dates = {'1': '2017-11-11', '2': '2017-10-11'}
change_dates can have multiple keys
I need to find all records from the database, where all values of the json field are greater than the current date
How I can do this?
This should work:
Post.where("change_dates->>'1' = ? or change_dates->>'2' = ?", Date.today, Date.today))

Finding records between date range

I'm trying to find records whose start and end date range over a particular date. Date is random and :start_date and :end_date are attributes of the prices entity.
date = Time.now
record_i_want = Price.where(date => :start_date .. :end_date)
Thank you.
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])
You want to select rows where your date is greater than the start_date and less than the end_date. You can specify the appropriate SQL where clause parameterized in your call to where like so:
Price.where([":date >= start_date AND :date <= end_date", {date: Date.today})
That will give you all the prices that match. If you know you'll get only one you can get it by calling first.
Price.where([":date >= start_date AND :date <= end_date", {date: Date.today}).first
Make any appropriate adjustment to the >= and <= if you want to exclude the start_date and/or the end_date from the results. If for example the Price is valid starting on the start_date but isn't valid through the end_date you can change the clause to:
":date >= start_date AND :date < end_date"
This should work:
def get_record_by_date(date)
Price.where([start_date.to_i < date.to_i] && [end_date.to_i > date.to_i])
end

How do I search for a date field after or before a certain date in Active Record

I know I can use a range to search where a date lies between 2 dates. For example:
User.where(:created_at => start_date..end_date )
But how would I do an open ended search for all dates before or after a date?
Thanks for any help
You can simply to this :
User.where("created_at >= ?", start_date )
Or this
User.where("created_at <= ?", end_date )

Comparing Time.now with model dates

my Project model has 2 datetime atttributes: start_date and end_date.
Now I want all projects where the current time is in between these dates.
I tried something like this with the start_date to start with:
#projects = Project.where(:start_date <= Time.now)
But this returns an error:
comparison of Symbol with Time failed
Any ideas? Thanks!
Unlike some ORMs, active record doesn't augment the symbol class with methods to allow expressions other than equality to be expressed in this way. You just have to do
Project.where('start_date <= ?', Time.now)
The squeal gem adds this sort of stuff and allows you to write
Project.where{start_date < Time.now}
You can't do this: :start_date <= Time.now. You're comparing a symbol and a date with the <= operator.
If you want to add a condition to your query, pass it as a string:
Project.where("start_date <= ?", Time.now);
Unfortunately, with a where clause comparing dates, you'll have to drop into SQL. Try something like this instead:
#projects = Project.where(['projects.start_date <= ?', Time.now])

RoR | Range of Dates not traversing multiple months

I'm trying to create an array of dates for a calendar where there are a few extra day for the next and previous month that will fill in the week.
Here is my current method to try and get the array
def calendar
selected_month = Date.civil((Time.zone.now.year).to_i, (Time.zone.now.month).to_i)
start_date = selected_month.beginning_of_month
start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)
end_date = selected_month.end_of_month
end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date
#only puts 1-30/31 and does not include the extra off set of days from start and end. :(
date_range = (start_date..end_date).to_a
end
The problem is the rang only start at 1 and goes to the end of the month even though the start and end days exceed that.
I'm not married to this way of getting the array so maybe you have a better whole idea?
You forgot to reassign the values of start_date and end_date.
start_date = selected_month.beginning_of_month
start_date = start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)
end_date = selected_month.end_of_month
end_date = end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date

Resources