Skip to content

Instantly share code, notes, and snippets.

@webdestroya
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save webdestroya/c5e466a87eb78656c8a0 to your computer and use it in GitHub Desktop.

Select an option

Save webdestroya/c5e466a87eb78656c8a0 to your computer and use it in GitHub Desktop.
Item.joins("LEFT JOIN comments ON comments.item_id=items.id AND comments.service='TRUE'")
.joins("LEFT JOIN comments c2 ON c2.item_id=items.id AND c2.service='TRUE' AND (COALESCE(c2.created_at, items.purchased_at) + INTERVAL items.service_interval YEAR) >= NOW() AND c2.id <> comments.id")
.where("(COALESCE(comments.created_at, items.purchased_at) + INTERVAL items.service_interval YEAR) < NOW()")
.where("c2.id IS NULL")
.where("items.service_interval IS NOT NULL AND items.service_interval > 0")
Item.joins("LEFT JOIN comments ON comments.item_id=items.id AND comments.service='TRUE'")
.joins("LEFT JOIN comments c2 ON c2.item_id=items.id AND c2.service='TRUE' AND (COALESCE(c2.created_at, items.created_at) + INTERVAL items.service_interval) >= NOW()")
.where("(COALESCE(comments.created_at, items.created_at) + items.service_interval) < NOW()")
.where("c2.id IS NULL")
.where("items.service_interval IS NOT NULL AND items.service_interval > 0")
Item.joins("LEFT JOIN comments ON comments.item_id=items.id AND comments.service='TRUE'")
.where("(COALESCE(comments.created_at, items.created_at) + items.service_interval) < NOW()")
.where("items.service_interval IS NOT NULL AND items.service_interval > 0")
# Item.joins(:comments)
# .where(comments: {service: true})
# .where("(comments.created_at + items.service_interval) < NOW()")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment