Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a less than today condition on a date in Rails ActiveRecord

I'm trying to figure out how to pull all the records in my set where their fields publish is true and expires is less than today. I have the following but I don't think the less than part is working, can someone please point me on the right track?

Announcement.where(publish: true, :expires < Date.today) 

Thanks in advance for your help

like image 356
Adam Avatar asked Dec 10 '11 16:12

Adam


People also ask

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is an ActiveRecord relation object?

The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.

What is active record pattern in rails?

The Active Record Pattern is used to access the data stored in a relational database. It allows us to create, read, update, and delete data from a database. It is particularly used for persistently stored data. The Active Record Pattern is a part of the MVC design pattern.


1 Answers

Try this:

Announcement.where("publish = ? AND expires < ?", true, Date.today) 
like image 60
leonardoborges Avatar answered Sep 22 '22 19:09

leonardoborges