if loaded? target.select(&:verified?). sort_by(:updated_at).last else where('verified_at IS NOT NULL'). order('updated_at DESC').first end end end end by @moro
foreign_key: :item_id, conditions: ['EXISTS(SELECT 1 FROM snippets s WHERE s.id = ? AND pastes.item_id = s.id)', 'Snippet'] belongs_to :pictures, readonly: true, foreign_key: :item_id, conditions: ['EXISTS(SELECT 1 FROM pictures s WHERE s.id = ? AND pastes.item_id = s.id)', 'Picture'] def item (...) end end
:pictures, (...) def item actual_item = association(item_type.underscore.to_sym) polymorphic_item = association(:item) if actual_item.loaded? && !polymorphic_item.loaded? polymorphic_item.target = actual_item.target end super end end
LIMIT 10 # => SELECT "snippets".* FROM "snippets" WHERE "snippets"."id" IN (1, 2, 3, 4, 5, 6) AND (EXISTS(SELECT 1 FROM "pastes" WHERE "pastes".item_type = 'Snippet' AND "pastes".item_id = "snippets".id)) # => SELECT "pictures".* FROM "pictures" WHERE "pictures"."id" IN (1, 2, 3, 4, 5, 6) AND (EXISTS(SELECT 1 FROM "pastes" WHERE "pastes".item_type = 'Picture' AND "pastes".item_id = "pictures".id))
"#{association} is not polymorphic association" unless association.options[:polymorphic] @association = association @types = types end def belong_to_them(ref_from) @types.each do |t| ref_from.belongs_to t, readonly: true, foreign_key: fk, conditions: condition_sql(t.to_s.classify.constantize) end end def define_scope(ref_from) ref_from.scope "with_#{@association.name}", ref_from.includes(*@types) end def override_accessor(ref_from) ref_from.class_eval <<-RUBY.strip_heredoc def #{@association.name} acutual_item = association(self[#{ft.dump}].underscore.to_sym) polymorphic_item = association(:#{@association.name}) if acutual_item.loaded? && !polymorphic_item.loaded? polymorphic_item.target = acutual_item.target end super end RUBY end private # aliasses def ft @association.foreign_type http://bit.ly/eager-loadable-polymorph
def #{@association.name} acutual_item = association(self[#{ft.dump} polymorphic_item = association(:#{@associa if acutual_item.loaded? && !polymorphic_it polymorphic_item.target = acutual_item.t end super end RUBY end private ͬͱίϯύΫτʹॻ͖͍ͨ! http://bit.ly/eager-loadable-polymorph
blogs ON blogs.id = posts.blog_id INNER JOIN user_blogs ON blogs.id = user_blogs.blog_id WHERE user_blogs.user_id = 1 AND (posts.created_at >= '2012-09-08 03:14:52.839676') Associationͱmerge AR 3.2Ͱඞཁ 4.0Ͱෆཁ
User.where(name: ‘tenderlove’) select * from users where name <> ‘tenderlove’ <= User.where(“name <>‘tenderlove’”) select * from users where name like ‘tender%’ <= User.where(“name like ‘tender%’”) RubyͷHash SQLͦͷͷ
int szOsFile; /* Size of subclassed sqlite3_file */ int mxPathname; /* Maximum file pathname length */ sqlite3_vfs *pNext; /* Next registered VFS */ const char *zName; /* Name of this virtual file system */ void *pAppData; /* Pointer to application-specific data */ int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, int flags, int *pOutFlags); int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); void (*xDlClose)(sqlite3_vfs*, void*); int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); int (*xSleep)(sqlite3_vfs*, int microseconds); int (*xCurrentTime)(sqlite3_vfs*, double*); int (*xGetLastError)(sqlite3_vfs*, int, char *); /* New fields may be appended in figure versions. The iVersion ** value will increment whenever this happens. */ }; Step 1: Read the API
flags end end class DATABase < SQLite3::VFS::File def initialize name, flags super @store = File.open(name, File::RDWR | File::CREAT) @offset = 0 if File.expand_path(__FILE__) == name @store.seek DATA.pos @offset = DATA.pos end end def close @store.close end def read amt, offset @store.seek(offset + @offset) @store.read amt end def write data, offset @store.seek(offset + @offset) @store.write data end def sync opts @store.fsync end def file_size File.size(@store.path) - @offset end end Stores data to after then __END__ part
Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport run Test328::Application.routes ੵ·ΕͯΔmiddlewareΛ֬ೝ
g.session_store :cookie_store, key: '_myapp_session' app.initialize! app.routes.draw { resources(:users) } class User < ActiveRecord::Base; end class ApplicationController < ActionController::Base; end class UsersController < ApplicationController def index @users = User.all render inline: ’<%= @users.map(&:name).join("\n") %>’ end end Object.const_set(:ApplicationHelper, Module.new)