$ rails g model User
belongs_to
has_one
| const memoize = (fn) => { | |
| let cache = {}, key; | |
| return (...args) => { | |
| key = JSON.stringify(args); | |
| return cache[key] || (cache[key] = fn.call(null, ...args)); | |
| } | |
| }; |
It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media
This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.
Install ImageMagick for image conversion:
brew install imagemagick
Install tesseract for OCR:
brew install tesseract --all-languages
Or install without --all-languages and install them manually as needed.
#!/bin/ruby --verion => 2.0.0-p353
In Ruby, self is a special variable that always references the current object.
| require 'spec_helper' | |
| describe Model do | |
| it "has a valid factory" do | |
| expect(build(:factory)).to be_valid | |
| end | |
| # Lazily load factory so its only used when needed. | |
| let(:factory_instance) { build(:parent) } |
| # PUT THIS IN THE `spec_helper.rb` file | |
| # <><><><><><><><><><><> | |
| RSpec.configure do |config| | |
| # Allows for shorthand version of FactoryGirl syntax. | |
| config.include FactoryGirl::Syntax::Methods | |
| #... | |
| end | |
| # <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> |
| Africa/Abidjan | |
| Africa/Accra | |
| Africa/Addis_Ababa | |
| Africa/Algiers | |
| Africa/Asmara | |
| Africa/Asmera | |
| Africa/Bamako | |
| Africa/Bangui | |
| Africa/Banjul | |
| Africa/Bissau |
| r = Rails.application.routes | |
| r.recognize_path "/children" | |
| r.methods - Object.methods |