0

I trying to write some condition to pull out objects from database:

Page.where(published: true).where("`published_at` <= current_date()").where("`publication_end` IS NULL OR `publication_end` > current_date()")

When i enter it in the rails console i have the following error:

 SELECT "pages".* FROM "pages" WHERE "pages"."published" = 't' AND (`published_at` <= current_date()) AND (`publication_end` IS NULL OR `publication_end` > current_date())
PG::SyntaxError: ERROR:  syntax error at or near "("
LINE 1: ...blished" = 't' AND (`published_at` <= current_date()) AND (`...
                                                             ^
: SELECT "pages".* FROM "pages"  WHERE "pages"."published" = 't' AND (`published_at` <= current_date()) AND (`publication_end` IS NULL OR `publication_end` > current_date())
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near "("
LINE 1: ...blished" = 't' AND (`published_at` <= current_date()) AND (`...
                                                             ^
: SELECT "pages".* FROM "pages"  WHERE "pages"."published" = 't' AND (`published_at` <= current_date()) AND (`publication_end` IS NULL OR `publication_end` > current_date())

I,m Using Postgresql Please help.

3
  • 1
    Did you simply try to remove the () after current_date? Commented Feb 19, 2014 at 15:54
  • current_date does not have the trailing "()", though "now()" does. Commented Feb 19, 2014 at 15:55
  • I hope those backticks are part of the ActiveRecord's syntax and not something that's sent to Postgres. Commented Feb 19, 2014 at 16:00

2 Answers 2

2

Remove the parentheses:

# select current_date();
ERROR:  syntax error at or near "("
LINE 1: select current_date();  

# select current_date;
    date
------------
 2014-02-19
(1 row)
Sign up to request clarification or add additional context in comments.

1 Comment

By way of explanation: current_date is a special SQL keyword specified in the ANSI SQL standard. It acts like a function that takes zero arguments, but unlike a function its syntax doesn't require or allow parentheses. Yet another example of the SQL standards committee making up weird syntax for fun.
-1

i write:

Page.where(published: true).where("published_at <= current_date").where("publication_end IS NULL OR publication_end > current_date")

and now i ok

Thanks :)

Comments

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.