ruby on rails - Active record get all instance that do not match condition -


i have following ar query returns array of rooms (the rooms not available in time period given) :

rooms = room.joins(:bookings).where("(bookings.start_date >= ? , bookings.start_date <= ?) or (bookings.end_date >= ? , bookings.end_date <= ?) or (bookings.start_date <= ? , bookings.end_date >= ?)", from, to, from, to, from, to) 

i want modify query returns other rooms; ones available in requested time period. right result givent following :

all_rooms = room.all available_rooms = all_rooms - rooms 

but directly want available rooms in single query. added .not doesnt give me right result :

rooms = room.joins(:bookings).where.not("(bookings.start_date >= ? , bookings.start_date <= ?) or (bookings.end_date >= ? , bookings.end_date <= ?) or (bookings.start_date <= ? , bookings.end_date >= ?)", from, to, from, to, from, to) 

how should modify query ?

let's say:

a = (bookings.start_date >= ? , bookings.start_date <= ?) b = (bookings.end_date >= ? , bookings.end_date <= ?)  c = (bookings.start_date <= ? , bookings.end_date >= ?) first_logic  = or b or c second_logic = not(first_logic) = not(a or b or c) = not(a) , not(b) , not(c) 

available_rooms condition of second_logic, like:

available_rooms = room.joins(:bookings)                       .where.not("bookings.start_date >= ? , bookings.start_date <= ?", from, to)                       .where.not("bookings.end_date >= ? , bookings.end_date <= ?", from, to)                       .where.not("bookings.start_date <= ? , bookings.end_date >= ?", from, to) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -