Search code examples
ruby-on-railsrubypostgresqlactiverecordpostgresql-9.3

Rails, Postgres, ActiveRecord query postgres based on columns value


I working with Rails and Postgresql and I'm trying to query my postgres db based on the value of a column.

To put it into perspective, I have an Events table in postgres and in that table I have some events that are recurring and some that aren't based on the column value of repeats.

If the events recur, then they will have a value of daily, weekly, monthly, etc. inside of the repeats column. Obviously, if it doesn't recur then the value will be set to null.

I would like to receive only those events that recur in the repeats column. So if it's null, then the query will skip over it.

I've looked through the ActiveRecord Query Interface but due to my inexperience, I was unable to find a helper that would do the trick for me.

Please let me know if more info is needed.

Any advice will be greatly appreciated. Thanks!


Solution

  • Event.where.not(repeats: nil)
    

    Alternatively, you can specify a sql stream

    Event.where("repeats is not null")