|
|
|
|
|
by pqdbr
1736 days ago
|
|
Great article. By the way, ActiveRecord's #merge is golden, and I'm under the impression it's not as mainstream as it should be. I use it extensively to avoid duplicating scope code. For instance: class Listing scope :active, -> { where("expired_at > ?", Date.current).where.not(suspended: true) }
endclass User has_many :listings
endSo instead of doing this (which is terrible): user.joins(:listings).distinct.where("listings.expired_at > ? AND listings.suspended != FALSE", Date.current) You can simply: user.joins(:listings).distinct.merge(Listing.active) Rails docs are amazing, but #merge doesn't get enough love. Maybe I'll issue a pull request to improve it with some examples like this and the ones from the article. |
|