Mark Locklear
Mark Locklear

Reputation: 5325

Rails where clause with between not working in mysql

I have the following query in rails...

@registrations = Registration.where(Orientation.where(class_date: @start_date..@end_date))

The query itself does not throw an error, however if I try to inspect @registrations do anything with the variable I get the error...

Cannot visit ActiveRecord::Relation

Any ideas?

Upvotes: 2

Views: 765

Answers (1)

xlembouras
xlembouras

Reputation: 8295

Orientation.where(class_date: @start_date..@end_date)

does the range query you like

SELECT * FROM orientations WHERE class_date BETWEEN <start_date> AND <end_date>

UPDATE

Registration.where(created_at: @start_date..@end_date).
  joins(:orientation).
  where('orientations.created_at' => @start_date..@end_date)

will make the following SQL which I think is what you need

SELECT registrations.* FROM registrations INNER JOIN orientations ON orientations.id = registrations.orientation_id WHERE (registrations.created_at BETWEEN '2014-02-17 16:01:41' AND '2014-02-19 16:01:41') AND (orientations.created_at BETWEEN '2014-02-17 16:01:41' AND '2014-02-19 16:01:41')

Upvotes: 4

Related Questions