I am trying to return records where the association is present or not:
I tried these areas:
class Booking < ActiveRecord::Base has_one :availability scope :with_availability, -> {where{availability.not_eq nil}} scope :without_availability, -> {where{availability.eq nil}} end
Try the following:
class Booking < ActiveRecord::Base has_one :availability scope :with_availability, -> { joins{availability} } scope :without_availability, -> { joins{availability.outer}.where{availability.id.eq nil} } end
Use instance methods instead
def with_availability availability.present? end def without_availability availability.blank? end
, , :
class Booking < ActiveRecord::Base has_one :availability scope :with_availability, -> {where(id: Availability.pluck(:booking_id))} scope :without_availability, -> {where.not(id: Availability.pluck(:booking_id))} end
, ( ):
Rails - has_one :
Source: https://habr.com/ru/post/1608888/More articles:Node.js performance and memory leak - javascriptWhat does the STL method name "rdbuf" mean? - c ++UWP - MessageDialog disables the application in Windows Phone and tablet mode - c #perform functions immediately upon opening R - rPhotoSwipe full screen mode suitable for window - javascriptcreateArrayOf AbstractMethodError - jdbcHow to sort multiple arrays of form fields in php? - sortingCrashplan Π½Π° FreeNAS ΠΎΡΡΡΡΡΡΠ²ΡΠ΅Ρ/var/lib/crashplan/.ui_info - nasXcode 7 / iOS 9: how to enable a self-signed certificate for NSURLSession - iosVisibility, Undo, and Timeout Capabilities CKQueryOperation - iosAll Articles