Skip to content

Instantly share code, notes, and snippets.

@zosiu
Created March 31, 2015 09:39
Show Gist options
  • Save zosiu/d8e1d1b8266ca56ca0a2 to your computer and use it in GitHub Desktop.
Save zosiu/d8e1d1b8266ca56ca0a2 to your computer and use it in GitHub Desktop.
has_many with date_part condition
# using the awesome squeel gem
has_many :shares, dependent: :destroy
has_many :stories, through: :shares
has_many(
:stories_this_month, -> do
where do
date_part('month', created_at).eq(Date.today.month) &
date_part('year', created_at).eq(Date.today.year)
end
end,
through: :shares,
source: :story,
class_name: 'Story'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment