Ruby on Rails 8.1.1 Module ActiveRecord::Integration::ClassMethods activerecord/lib/active_record/integration.rb Defines your model’s to_param method to generate “pretty” URLs using method_name, which can be any attribute or method that responds to to_s. class User < ActiveRecord::Base to_param :name end user = User.find_by(name: 'Fancy Pants') user.id # => 123 user_path(user) # => "/users/123-fan
I have an issue with the uniq method. I have a database with a table as below. MariaDB [hpbx_development]> select * from asterisk_commands; +-------+-----------------+---------------------+---------------------+-----------+----------+-------------------+ | id | command | created_at | updated_at | completed | override | virtual_server_id | +-------+-----------------+---------------------+----------
Enable the query cache within the block if Active Record is configured. If it’s not, it will execute the given block. # File activerecord/lib/active_record/query_cache.rb, line 10 def cache(&block) if connected? || !configurations.empty? pool = connection_pool was_enabled = pool.query_cache_enabled begin pool.enable_query_cache(&block) ensure pool.clear_query_cache unless was_enabled end else yiel
# == A any? B blank?, build C cache_key, cache_key_with_version, cache_version, create, create!, create_or_find_by, create_or_find_by! D delete, delete_all, delete_by, destroy, destroy_all, destroy_by E eager_loading?, empty?, encode_with, explain F find_or_create_by, find_or_create_by!, find_or_initialize_by I initialize_copy, insert, insert!, insert_all, insert_all!, inspect J joined_includes_va
Set.new([:where, :select, :group, :order, :lock, :limit, :offset, :joins, :left_outer_joins, :annotate, :includes, :eager_load, :preload, :from, :readonly, :having, :optimizer_hints, :with]) Returns a new relation, which is the logical intersection of this relation and the one passed as an argument. The two relations must be structurally compatible: they must be scoping the same model, and they mu
# File activerecord/lib/active_record/secure_token.rb, line 61 def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH) SecureRandom.base58(length) end Example using has_secure_token # Schema: User(token:string, auth_token:string) class User < ActiveRecord::Base has_secure_token has_secure_token :auth_token, length: 36 end user = User.new user.save user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
This guide teaches you how to validate Active Record objects before saving them to the database using Active Record's validations feature. After reading this guide, you will know: How to use the built-in Active Record validations and options. How to check the validity of objects. How to create conditional and strict validations. How to create your own custom validation methods. How to work with th
マイグレーション(migration)はActive Recordの機能の1つであり、データベーススキーマが長期にわたって進化を安定して繰り返せるようにするための仕組みです。マイグレーション機能のおかげで、スキーマ変更を生SQLで記述せずに、Rubyで作成されたマイグレーション用のDSL(ドメイン固有言語)を用いてテーブルの変更を簡単に記述できます。 このガイドの内容: マイグレーション作成でどんなジェネレータを利用できるか Active Recordでどんなデータベース操作用メソッドが提供されているか 既存のマイグレーションの変更方法や、スキーマの更新方法 マイグレーションとスキーマファイルschema.rbの関係 参照整合性を維持する方法 1 マイグレーションの概要 マイグレーションは、再現可能な方法でデータベーススキーマを継続的に進化させる便利な方法です。 マイグレーションではRub
# File activerecord/lib/active_record/relation/calculations.rb, line 122 def async_average(column_name) async.average(column_name) end
OMG! Happy Thursday! I am trying to be totally enthusiastic, but the truth is that I have a cold, so there will be fewer uppercase letters and exclamation points than usual. Anyway, I want to talk about database connection management in ActiveRecord. I am not too pleased with its current state of affairs. I would like to describe how ActiveRecord connection management works today, how I think it s
1 Active Recordについて Active Recordとは、MVCで言うところのM、つまりモデルの一部であり、データとビジネスロジックを表現するシステムの階層です。Active Recordは、データベースに恒久的に保存される必要のあるビジネスオブジェクトの作成と利用を円滑に行なえるようにします。 RailsのActive RecordがActive Modelとどこが違うかというと、Active Modelは背後にデータベースが「なくてもよい」Rubyオブジェクトを用いてデータをモデル化するときに主に用いられます。Active RecordとActive Modelは、どちらもMVCのMの一部ですが、Active Modelは独自のプレーンなRubyオブジェクト(PORO)としても利用できます。 「Active Record」は、ソフトウェアアーキテクチャパターンを指すという
1 オブジェクトのライフサイクル Railsアプリケーションの通常の操作中に、オブジェクトが作成・更新・破棄されることがあります。Active Recordは、このオブジェクトのライフサイクルへのフックを提供することでアプリケーションとそのデータを制御できます。 コールバックを使うと、オブジェクトの状態の変更「前」または変更「後」にロジックをトリガーできます。コールバックとは、オブジェクトのライフサイクルの特定の瞬間に呼び出されるメソッドのことです。コールバックを使えば、Active Recordオブジェクトがデータベースで初期化・作成・保存・更新・削除・バリデーション・読み込みのたびに実行されるコードを記述できます。 class BirthdayCake < ApplicationRecord after_create -> { Rails.logger.info("Congratula
1 Active Recordクエリインターフェイスとは? 生のSQLを使ってデータベースのレコードを検索することに慣れた人がRailsに出会うと、Railsでは同じ操作をずっと洗練された方法で実現できることに気付くでしょう。Active Recordを使うことで、SQLを直に実行する必要はほぼなくなります。 Active Recordは、ユーザーに代わってデータベースにクエリを発行します。発行されるクエリは多くのデータベースシステム(MySQL、MariaDB、PostgreSQL、SQLiteなど)と互換性があります。Active Recordを使えば、利用しているデータベースシステムの種類にかかわらず同じ記法を使えます。 本ガイドのコード例では以下のモデルを使います。 class Book < ApplicationRecord belongs_to :supplier belong
1 関連付けの概要 Active Recordの「関連付け(アソシエーション: association)」を使うと、モデル間のリレーションシップを定義できます。関連付けは特殊なマクロスタイルの呼び出しとして実装されており、モデル同士をどのように関連させるかをRailsに手軽に指定できます。これにより、データの管理がより効率的になり、一般的なデータ操作がシンプルで読みやすくなります。 マクロスタイルの呼び出しは、実行時に他のメソッドを動的に生成・変更するメソッドであり、Railsでのモデルの関連付けの定義など、簡潔で表現力豊かな機能の宣言を可能にします。たとえばhas_many :commentsのように記述します。 関連付けを設定すると、Railsが2つのモデルのインスタンス同士の主キー(primary key)と外部キー(foreign key)のリレーションシップや管理を支援し、データ
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く