# frozen_string_literal: true module Naming extend ActiveSupport::Concern DEFAULT_NAME = "Default Name" # scope, callback, relation はここに定義 included do before_create :assign_default_name, unless: :name? scope :with_name, -> (names) { where(name: names) } end # class メソッド class_methods do # cattr_reader 使ってもいいですが例なのであえて冗長に書いてます def default_name DEFAULT_NAME end end def assign_default_name self.name
複数のモデルにステータスなどの共通した値と処理を持たせたい!と思ってRails4.1のenumを使いつつ、concernなmoduleに切り出そうと思ったら、enumはActiveRecord所属だったので、困ってしまったときのメモ。 しょうがないので、ActiveRecordを継承したクラスを作ってそこに処理をまとめようとした。 class StatusRecord < ActiveRecord::Base before_create :set_status enum status: {draft: 0, published: 1} private def set_site self.status = some_helper_method end end class Article < StatusRecord ~snip~ end そしたら、statusってテーブルがねーよッ、というニ
Rails4.0からadd_reference使えるようになってた. rails g migration AddPiyoToHoges piyo:refernces とかすると class AddPiyoToHoges < ActiveRecord::Migration def change add_reference :hoges, :piyo, index: true end end なファイルが出来て rails db:migrate するとdb/schema.rbで create_table "hoges", force: true do |t| t.integer "piyo_id" end add_index "hoges", ["piyo_id"], name: "index_hoges_on_piyo_id", using: :btree みたいになる. 便利. 参考 ad
Generating Generating $ rails g model User Using models Query methods items = Model .where(first_name: 'Harvey') .where('id = 3') .where('id = ?', 3) .order(:title) .order(title: :desc) .order("title DESC") .reorder(:title) # discards other .order's .rewhere(...) # discards other .where's .limit(2) .offset(1) .uniq See: QueryMethods Advanced query methods items = Model .select(:id) .select([:id, :
This webpage was generated by the domain owner using Sedo Domain Parking. Disclaimer: Sedo maintains no relationship with third party advertisers. Reference to any specific service or trade mark is not controlled by Sedo nor does it constitute or imply its association, endorsement or recommendation.
Active Record Active Record objects don’t specify their attributes directly, but rather infer them from the table definition with which they’re linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen au
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く