Skip to content

Instantly share code, notes, and snippets.

@tybenz
Created April 2, 2014 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tybenz/9944205 to your computer and use it in GitHub Desktop.
Save tybenz/9944205 to your computer and use it in GitHub Desktop.
# grab a page
page = Page.find(1)
# grab the page's list of content_items and eager load each page's markups
content_items = page.content_items.includes(:markups)
# first content_item
content_item = content_items.first
# returns a collection of markups - doesn't output a SQL query
content_item.markups
# returns the list of markup types - DOES output a SQL query for the markups
content_item.html
class ContentItem < ActiveRecord::Base
has_many :markups
def html
markups.map do |markup|
markup.type
end
end
end
class Markup < ActiveRecord::Base
# has start_index, end_index and type
end
class Page < ActiveRecord::Base
has_many :content_items
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment