Deleted articles cannot be recovered. Draft of this article would be also deleted. Are you sure you want to delete this article?
Rails 5.2.0 で入ったActiveModel::Attributes API 最高ですよね。 でもカスタムタイプのドキュメントが見つからないんですよね。 ActiveRecord::Attributes API のカスタムタイプ ならあるのですが。 ソースコード見たところ簡単に作れるのがわかったので紹介します。 まず型の登録部分ですが、lib/active_model/type.rb で定義されています。 また、ActiveModelで使われているデフォルトタイプの実装を見ると cast_value メソッドがあればよさそうです。 ActiveRecord::Attributes API と同様に実装します。 class MoneyType < ActiveModel::Type::Integer def cast_value(value) if !value.kind_of?(
概要 携わっている案件で作成しているRailsアプリケーションでは、JSONシリアライザとしてRablを採用していました。しかしどうもRablが遅い。 そこで、さまざまなシリアライザを比較してみることにしました。 世間では"ActiveModel Serializerが圧倒的に早いぞ!"というのをよく見るが、だいたいの検証記事はひとつのモデルのインスタンスをそのままシリアライズするような場合。 関連するモデルの情報を含めたり、モデルの属性から算出する値を含めたりする場合の性能比較は見つかりません。 そこで、より現実に即した状況で性能を比較検討してみましたというものです。。 全選手入場!! 比較対象は次のよっつ。 ActiveModel::Serializer Grape::Entity Jbuilder (個人的には一番書きやすくて好き) Rabl バージョンはそれぞれ active_mo
Ruby on Rails 5.2.0.beta2が出て久しい。 僕はRails大好きっ子なので 社内の新規事業や、社外のお手伝いしているベンチャー企業では もちろん、Rails5.2を使って開発をしている。 すでに、新しいRailsに関する記事はいくつか挙がっているが 個人的に、一番インパクトが大きかったのはActiveModel::Attributesが導入されたことである。 待望のActiveModel::Attributes ActiveModel::Attributesでなにが変わったの?というと、 いままでActiveRecordでしか使えなかった一部の機能が、ActiveModelでも使えるようになっただけである。 いや、しかし! 今までのActiveModelの最大の弱点は まさにこの ActiveModel::Attributes が無かったことだと思う。 それが、Rai
Let’s continue from where I left off in Part 1 where I quickly described how you can extract reusable model logic from a Rails app into a testable Rubygem. If I were building a secondary Rails app connecting directly to the same database as the first, I could just add the dependency to the extracted gem: gem 'central-support', github: 'Codeminer42/cm42-central-support', branch: 'master', require:
Active Model Validator¶ ↑ A simple base class that can be used along with ActiveModel::Validations::ClassMethods.validates_with class Person include ActiveModel::Validations validates_with MyValidator end class MyValidator < ActiveModel::Validator def validate(record) if some_complex_logic record.errors.add(:base, "This record is invalid") end end private def some_complex_logic # ... end end Any c
Active Model Basic Model¶ ↑ Allows implementing models similar to ActiveRecord::Base. Includes ActiveModel::API for the required interface for an object to interact with Action Pack and Action View, but can be extended with other functionalities. A minimal implementation could be: class Person include ActiveModel::Model attr_accessor :name, :age end person = Person.new(name: 'bob', age: '18') pers
Google グループでは、オンライン フォーラムやメール ベースのグループを作成したり、こうしたフォーラムやグループに参加したりすることで、大勢のユーザーと情報の共有やディスカッションを行うことができます。
オブジェクトがDBに保存される前に、そのデータが正しいかどうかを検証する仕組みをバリデーションといいますが、 RailsでActiveRecordを使ってそれを実現するにあたってよく使いそうなのをまとめます。 以下のメソッドにおいてはバリデーションがトリガされます。 create create! save save! update update! 以下のメソッドにおいてはバリデーションはスキップされます。 decrement! decrement_counter increment! increment_counter toggle! touch update_all update_attribute update_column update_columns update_counters Railsでのバリデーショントリガ Railsではvalid?メソッドを実行するとバリデーションが実
This guide will provide you with what you need to get started using Active Model. Active Model provides a way for Action Pack and Action View helpers to interact with plain Ruby objects. It also helps to build custom ORMs for use outside of the Rails framework. After reading this guide, you will know: What Active Model is, and how it relates to Active Record. The different modules that are include
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く