2.2.2+
rails new my-app-api --api
class MoneyType < ActiveRecord::Type::Integer
def type_cast(value)
if value.include?('$')
price_in_dollars = value.gsub(/\$/, '').to_f
price_in_dollars * 100
else
value.to_i
end
end
end
class StoreListing < ActiveRecord::Base
attribute :price_in_cents, MoneyType.new
end
store_listing = StoreListing.new(price_in_cents: '$10.00')
store_listing.price_in_cents # => 1000
class ApplicationRecord < ActiveRecord::Base
self.abstract_class =true
include MyCustomErrorsModule
end
class Product < ApplicationRecord
end
MessagesController.render(partial: 'messages/message',
locals: { message: params[:message][:body] })
Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)
file system monitor using mtimes
evented file system monitor
listen gem
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end
# Rails 4
t.references :user, index: true, foreign_key: true
# Rails 5
t.references :user, foreign_key: true