rails

順序付きのHash OrderedHash

$ script/console Loading development environment (Rails 2.3.2) >> RUBY_VERSION => "1.8.7" >> ActiveSupport::OrderedHash.new(:a => 1, :b => 2) => #<OrderedHash {}> >> ActiveSupport::OrderedHash.new([[:a, 1], [:b, 2]]) => #<OrderedHash {}> >> hash = ActiveSupport::OrderedH</orderedhash></orderedhash>…

@reminders.map(&:id)

app/controllers/reminders_controller.rb from authorNari's brushup - GitHub @reminders.map(&:id) 動作 ruby 1.8.7 irb ○ ruby 1.8.6でRails 2.3.2 script/console ○ ruby 1.8.6 irb TypeError: wrong argument type Symbol (expected Proc) 別の書き方…

Authlogic OpenID 1.0.3

Ruby ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] Ruby on Rails 2.3.2 Authlogic 2.0.11 Authlogic OpenID 1.0.3

Authlogic 2.0.11

Authlogic Example App - Tutorial on how to create this app and easily setup Authlogic Ruby ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] Ruby on Rails 2.3.2 Authlogic 2.0.11

NetBeans 6.5.1用のパッチ

Rails 2.3.2 で Mongrel や WEBrick をサーバーとして使用するとその起動が NetBeans から認識できず、ブライザが起動しなかったり、すでに起動してあるブラウザの場合はページを更新できないという問題があります。 … 担当の Mononen が NetBeans 6.5.1 用…

Polymorphic Association

class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #.…

Rails 2.3 : Nested Attributes / Nested Object Forms

accepts_nested_attributes_for / fields_for Nested attributes allow you to save attributes on associated records through the parent. automatic (and atomic) saving of a record together with its associated children, child-aware validations, a…

Rails 2.3 : Nested Transactions

transaction(:requires_new => true) If you want a transaction to be nested, you must explicitly add the :requires_new option. User.transaction do User.create(:username => 'Admin') User.transaction(:requires_new => true) do User.create(:user…

Rails 2.3 : Dynamic Scopes

scoped_by_ Order.scoped_by_customer_id(12) Order.scoped_by_customer_id(12).find(:all, :conditions => "status = 'open'") Order.scoped_by_customer_id(12).scoped_by_status("open") Ruby on Rails 2.3 Release Notes - 4.3 Dynamic Scopes

Rails 2.3 : Default Scopes

default_scope(options = {}) class Article < ActiveRecord::Base default_scope :order => 'created_at DESC' end Article.find(:all) #=> "SELECT * FROM `articles` ORDER BY created_at DESC" Article.with_exclusive_scope { find(:all) } #=> "SELECT…

Rails 2.3 : Batch Processing

find_in_batches / find_each Invoice.find_in_batches(:include => :invoice_lines) do |invoices| export.add_invoices(invoices) end User.find_each do |user| NewsLetter.weekly_deliver(user) end Ruby on Rails 2.3 Release Notes - 4.5 Batch Proces…

Rails 2.3 : Object#try

you can avoid nil-checking @person.try(:name) # @person ? @person.name : nil @people.try(:collect) {|p| p.name} Person.try(:find, 1) Product.find_by_price(4.95).try(:name) Ruby on Rails 2.3 Release Notes - 7.1 Object#try Class: Object Rail…

rescue_from

Rescue exceptions raised in controller actions. class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, :with => :route_not_found private def route_not_found(exception) render :text => exception.mes…

Rails 2.2 : Memoization

Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. extend ActiveSupport::Memoizable def full_name "#{first_name} #{last_name}" end memoize :full_name Ruby on Rails 2.2 Release Notes - 9.…

send_file

ファイルのダウンロード send_file '/path/to.jpeg', :type => 'image/jpeg' Module: ActionController::Streaming 画像やファイルをダウンロードさせる | メモ帳

RSpec on Rails で spec_server is already running.

rake spec:server:startで、spec_serverが動いていないのに spec_server is already running.と言われたら、tmp/pids/spec_server.pid を削除する

RSpec 1.2.0

RSpecとRSpec on Railsが1.2.0にバージョンアップして、設定方法が変更された config.gem (for Rails) - rspec - GitHub Upgrades - rspec - GitHub spec_server + autospec == nearly pure BDD joy - rspec - GitHub テスト $ rake spec:server:start $ rak…

Rails 2.3.2 の Application Templates

Rails 2.3.2 の template_runner.rb railties/lib/rails_generator/generators/applications/app/template_runner.rb at 39ff550fa88da9a22d8c21ca872f5e4d0d83f8d4 from rails's rails - GitHub

OpenID Authentication

いますぐ使えるOpenID:第4回 Railsで作るOpenID対応アプリケーション実践(前編)|gihyo.jp $ rails openid_test $ cd openid_test/$ script/plugin install git://github.com/rails/open_id_authentication.git $ rake open_id_authentication:db:create$…

ActiveRecord Validation, Callback, Observer

Active Record Validations and Callbacks - Ruby on Rails guides trigger validations create create! save save! update update_attributes update_attributes! skip validations save(false) update_all update_attribute update_counters decrement! de…

Cygwin + Rails + SQLite3

Cygwin Ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] SQLite3 3.6.2 RubyGems 1.3.1 Ruby on Rails 2.2.2 sqlite3-ruby 1.2.4 RSpec 1.1.12 RSpec on Rails 1.1.12 rcov 0.8.1.2.0 ZenTest 4.0.0 Cygwin [Devel]-[cygport] [Devel]-[gcc] [Interpre…

メール設定をYAMLファイルから読み込む

Typo 5.2 (MIT-LICENSE) config/environment.rb if RAILS_ENV != 'test' begin mail_settings = YAML.load(File.read("#{RAILS_ROOT}/config/mail.yml")) ActionMailer::Base.delivery_method = mail_settings['method'] ActionMailer::Base.server_settings…

RSpec + RSpec on Rails

関連 http://d.hatena.ne.jp/mpen/20090318/p1 install gem install rspec gem install rspec-railsrspec と rspec-rails は同じバージョンであること 使い方 rails rspec_test cd rspec_test ruby script/generate rspec ruby script/generate rspec_model …

WindowsでSQLite 3のインストール

sqlite3-ruby http://rubyforge.org/frs/shownotes.php?release_id=25302 1.2.4のWindows用はないので、1.2.3を使用する gem list sqlite3-ruby --remote --all gem install sqlite3-ruby --version 1.2.3 --remote SQLite3 http://www.sqlite.org/download.…

Rails 2.2 の国際化

Controller flash[:notice] = I18n.t( :updated_success, :default => '{{model}} was successfully updated.', :model => Blog.human_name, :scope => [:railties, :scaffold] ) I18n.t 'date.formats.short' I18n.t 'short', :scope => 'date.formats' Vie…

restful-authentication と aasm のインストールと設定

使用環境 OS Windows XP Ruby 1.8.6 mswin32 Ruby on Rails 2.2.2 MySQL Community Server 5.0.77 NetBeans 6.5 Mongrel 1.1.5 aasm のインストール gem sources -a http://gems.github.comgem install rubyist-aasm Git(MinGW版)のインストール インストー…

config/routes.rb

使用環境 Ruby on Rails 2.2.2 map.root map.root :controller => 'users' root / {:action=>"index", :controller=>"users"} map.connect map.connect 'articles/:year/:month/:day', :controller => 'articles', :action => 'find_by_date', :year => /\d{…

config/routes.rb : map.resources

使用環境 Ruby on Rails 2.2.2 map.resources map.resources :coms .:format は省略 coms GET /coms {:action=>"index", :controller=>"coms"} POST /coms {:action=>"create", :controller=>"coms"} new_com GET /coms/new {:action=>"new", :controller=>"…

Ruby on Rails 2.0入門:Flickerベースのアプリケーションの作成

http://japan.internet.com/developer/20080606/26.html 使用環境 Ruby 1.8.6 mswin32 Ruby on Rails 2.2.2 MySQL Community Server 5.0.77 NetBeans 6.5 Mongrel 1.1.5 respond_to scaffold respond_to do |format| format.html # index.html.erb format.xm…

Ruby on Rails のチュートリアル

Ruby on Rails チュートリアル集 http://diaspar.jp/node/81 NetBeans6.1によるRuby on Rails開発環境構築手順書 http://codezine.jp/article/detail/2578 http://d.hatena.ne.jp/mpen/20090224/p1 Ruby のブログを 10 分で作成 http://www.netbeans.org/kb/…