Reputation: 836
I am having a date filters named start_date and end_date and I have to sort the Contacts based on this filters and sort according to created_at (a date field).
This is my method where I am listing all the records:
def execute_records
@items = []
if @params[:category_id] && !@params[:category_id].empty?
contacts = Contact.joins(:company).where(active: true, account_id: @user.id, companies: {category_id: @params[:category_id], active: true})
else
contacts = Contact.joins(:company).where(active: true, account_id: @user.id, companies: {active: true})
end
contacts.includes(company: [:addresses, :category, :lead_source]).each do |contact|
@items << Report.new(contact) if contact.is_a?(Contact)
end
end
Here in the above method I just wanted to add a condition where created_at is in between start_date and end_date. created_at is field in contacts table.
Please help me..
Upvotes: 0
Views: 34
Reputation: 644
I think you need this:
Contact.joins(:company).where(active: true, account_id: @user.id, companies: {created_at: Time.new(params[:start_date] || 0)..Time.new(params[:end_date] || Time.new(3000))})
Upvotes: 1