Skip to content
irxground edited this page Jun 7, 2013 · 10 revisions

Ruby on Rails

links

Memo

  • テーブル名の複数形をなくす場合
ActiveRecord::Base.pluralize_table_names = false

Handled Exceptions

Status Code Exception Reason
404 NotFound ActionController::RoutingError routes.rbが解決できなかった
404 NotFound AbstractController::ActionNotFound routes.rbで指定されたActionが見つからなかった
405 Method Not Allowed ActionController::MethodNotFound 不明
501 Not Implemented ActionController::NotImplemented 不明、直接この例外を投げるべきか?
406 Not Acceptable ActionController::UnknownFormat HTMLを返すActionにJSONを要求したなど
422 Unprocessable Entity ActionController::InvalidAuthenticityToken
400 Bad Request ActionController::BadRequest 不明
404 NotFound ActiveRecord::RecordNotFound find(id)で見つからなかった場合
409 Conflict ActiveRecord::StaleObjectError 楽観ロックで競合した場合
422 Unprocessable Entity ActiveRecord::RecordInvalid 設定したvalidationに失敗したとき。
422 Unprocessable Entity ActiveRecord::RecordNotSaved 不明(validationで失敗したときらしいが)

Rails Generators

Creating and Customizing Rails Generators & Templates

同じ ModelGenerator クラスであっても、 ActiveRecord::Generators::ModelGeneratorRails::Generators::ModelGenerator の二種類があることに注意。

継承

ActiveRecord::Generators::Base ActiveRecord::Generators::MigrationGenerator

Rails::Generators::MigrationGenerator

ActiveRecord - Association

def belongs_to(name, options = {})
  Builder::BelongsTo.build(self, name, options)
end
class ActiveRecord::Associations::Builder::Association
  def self.build(model, name, options)
    new(model, name, options).build
  end
  def initialize(model, name, options)
    @model, @name, @options = model, name, options
  end
  def build
    validate_options
    reflection = model.create_reflection(self.class.macro, name, options, model)
    define_accessors
    reflection
  end
Clone this wiki locally