Created
April 2, 2014 22:07
-
-
Save tybenz/9944205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContentItem < ActiveRecord::Base | |
has_many :markups | |
def html | |
markups.map do |markup| | |
markup.type | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Markup < ActiveRecord::Base | |
# has start_index, end_index and type | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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