Skip to content

Instantly share code, notes, and snippets.

@tybenz
Created Apr 2, 2014
Embed
What would you like to do?
# 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