Skip to content

Instantly share code, notes, and snippets.

@mhenrixon
Created November 19, 2012 10:04
Show Gist options
  • Save mhenrixon/4109936 to your computer and use it in GitHub Desktop.
Save mhenrixon/4109936 to your computer and use it in GitHub Desktop.
Where to add query methods?
# 1
List.fetch_by_slug(@site, 'mys-slug')
# 2 - adding query methods on the collection
@site.lists.fetch_by_slug('my-slug')
# How achieved
class Site < ActiveRecord::Base
has_many :lists do
def fetch_by_slug(slug)
includes(:rows).where(slug: slug)
end
end
end
# 3 - query methods on the parent
@site.fetch_list_by_slug('my-slug')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment