Created
October 20, 2009 10:03
-
-
Save boffbowsh/214139 to your computer and use it in GitHub Desktop.
Allows you to store blobs off-model for performance reasons, but still access them in a pretty way.
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 ProductPage < ActiveRecord::Base | |
has_many :text, :class_name => 'ProductPageContent' do | |
def [] name | |
name = name.to_s | |
find_by_field_name(name).body | |
end | |
def []= name, body | |
name = name.to_s | |
contents = find_or_initialize_by_field_name name | |
contents.write_attribute(:body, body) | |
contents.save | |
body | |
end | |
end | |
def text_attributes= attrs | |
attrs.each { |key,value| self.text[key]=value } | |
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
>> ProductPage.first.text[:full_description] = 'Hello' | |
ProductPage Load (0.3ms) SELECT * FROM `product_pages` LIMIT 1 | |
ProductPageContent Load (0.2ms) SELECT * FROM `product_page_contents` WHERE (`product_page_contents`.`field_name` = 'full_description') AND (`product_page_contents`.product_page_id = 430) LIMIT 1 | |
ProductPageContent Create (0.2ms) INSERT INTO `product_page_contents` (`updated_at`, `body`, `product_page_id`, `field_name`, `created_at`) VALUES('2009-10-20 09:57:34', 'Hello', 430, 'full_description', '2009-10-20 09:57:34') | |
=> "Hello" | |
>> ProductPage.first.text[:full_description] | |
=> "Hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment