Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: column reference "updated_at" is ambiguous with Postgres

I am trying to query the database with a DateTime range for the 'updated_at' field. The front end sends queries in a JSON array:

["2015-09-01 00:00:00","2015-10-02 23:00:00"]

At the Rails controller, I parse the two strings to DateTime using:

start_date = DateTime.parse(params[:date_range_arr][0])
end_date = DateTime.parse(params[:date_range_arr][1])

#...
@events = @events.where('updated_at BETWEEN ? AND ?,
       start_date, end_date
)

The queries show:

WHERE (updated_at BETWEEN '2015-09-01 00:00:00.000000' AND '2015-10-02 23:00:00.000000')

And the errors are:

ActionView::Template::Error (PG::AmbiguousColumn: ERROR:  column reference "updated_at" is ambiguous
LINE 1: ...texts"."id" = "events"."context_id" WHERE (updated_at...
like image 679
TonyGW Avatar asked Oct 02 '15 18:10

TonyGW


1 Answers

Do you have by any chance a default scope with a join or an include on the Event model or in the code above what's included in the original question?

Either way, you simply need to be more specific with your query as follow:

#...
@events = @events.where('events.updated_at BETWEEN ? AND ?,
   start_date, end_date
)
like image 146
Alexandre Voyer Avatar answered Nov 20 '22 00:11

Alexandre Voyer