2

I am new to rails - any help would be much appreciated.

  • I have a list of created job adverts
  • Advert is a model
  • In the schema Advert is a table with the columns title, content & deadline
  • I would like to only display adverts whose deadline are '= or >' than Date.current (active adverts)

Unfortunately I am unsure how to do it. I tried the below in the console:

active_adverts = Advert.where(:deadline > Date.current)

Could one kindly advise me on how to display adverts that have a deadline greater than the current date?

1 Answer 1

6

You can use the following syntax:

active_adverts = Advert.where('deadline >= ?', Date.current)    

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

You should also check out scopes. If you create a scope in your Advert model like: scope :active, -> { where("deadline >= ?", Date.current) } then you can re-use whenever you are doing an activerecord query on Adverts e.g. Advert.active or if a user has_many adverts @user.adverts.active

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.