2013-12-22から1日間の記事一覧

save/save! create/create!

前提情報 create_table "products", force: true do |t| t.string "title" t.text "description" t.string "image_url" t.decimal "price", precision: 8, scale: 2 t.datetime "created_at" t.datetime "updated_at" end class Product < ActiveRecord::Bas…

new と build

自身で改めて class Cart < ActiveModel has_many :line_items end class LineItems < ActiveModel belongs_to :cart end [1] Cart.new は OK Cart.build は NG [2] c = Cart.new して c.line_items.new, c.line_items.build は OK [3] そもそも build は ne…

find と find_by の違い と 補足

概要 User のレコードに、存在しない session[:id] を find, find_by したとき User.find(999) #=> ActiveRecord::RecordNotFound User.find_by(id: 999) #=> nil になる。 ファインダ find id などの主キーに find する、ということは 目的のレコードが存在…