2009-03-01から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…

croc インストールしたgemのドキュメントをブラウザでインクリメンタル検索

install $ gem install croc Successfully installed croc-1.0.0 1 gem installed Installing ri documentation for croc-1.0.0... Installing RDoc documentation for croc-1.0.0... $ croc /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in …

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…

Git

git

初期設定 $ git config --global user.name "XXXX" $ git config --global user.email XXX@example.com $ git config --global core.editor "'/cygdrive/d/XXX/XXX.exe' -CODE=4" $ git config --global color.diff auto $ git config --global color.status…