Range data for string?

I want to find a date in a column that is within this range:

date: 01-01-2018..08-01-2018

I tried this migration:

class AddColumnDataInHomeTask < ActiveRecord::Migration[5.1]
  def change
    add_column :home_tasks, :date, :string
  end
end

No error is raised, but it does not work. There is a problem with the type:

:string

Upvotes: 0

Views: 154

Answers (1)

Sean Kennedy
Sean Kennedy

Reputation: 411

First, you'll have to store the dates as the date type, not a string, in order to perform the kind of "between/range" query as you've described.

class AddColumnDataInHomeTask < ActiveRecord::Migration[5.1]
  def change
    add_column :home_tasks, :date, :date
  end
end

Then, consider this query:

records = HomeTask.where(:date => start_date..end_date)

Upvotes: 1

Related Questions