Skip to content

Instantly share code, notes, and snippets.

Please note this method is not atomic, it runs first a SELECT, and if there are no results an INSERT is attempted. If there are other threads or processes there is a race condition between both calls and it could be the case that you end up with two similar records.

Whether that is a problem or not depends on the logic of the application, but in the particular case in which rows have a UNIQUE constraint an exception may be raised, just retry:

begin
  CreditAccount.find_or_create_by(user_id: user.id)
rescue ActiveRecord::RecordNotUnique
  retry
end

ujs 會利用到data-type 這個keyword data attributes, 所以當你overwriate 掉時, 你會發現送出的request, header 什麼都不吃accept /, 所以無論你return 什麼他都會看不懂, 當然你預期中的的event (ajax:success) 也不會發生, 會跑到ajax:error 去

<%= form_for @task, :remote => true, :data => {:type => 'casue_problem'} %>
# 單純用來決定該有哪些 interface 和一些是在我們這邊處理的邏輯
class PaymentGateway
attr_reader :email
SUBSCRIPTION_AMOUNT = 10.to_money
def initialize(user)
@email = user.email
end

Why We Design

  • If this application need never change. design does not matter.
  • In the absence of design, unmanaged dependencies. Changing one object forces change upon its collaborators, which in turn, forces change upon its collaborators, ad infinitum

Judging Design

  • Easy to change.

How...

  • Classes with a Single Responsibility
  • Managing Dependencies
# execute ruby code
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))

# send self be context in the block
self.instance_eval(&block)

# create method
Person.instance_eval do
 # def class_method
  • The actual rendering is done by subclasses of ActionView::TemplateHandlers.
  • You can use the :location option to set the HTTP Location header
  • head :created, location: photo_path(@photo)
@ilake
ilake / routing.md
Last active December 20, 2015 00:49
# lib/action_dispatch/routing/mapper.rb
# everything under Foo::Application.routes.draw do
# is called by ActionDispatch::Routing::Mapper instance
# The draw method is defined in lib/action_dispatch/routing/route_set.rb
      def draw(&block)
        clear! unless @disable_clear_and_finalize
        eval_block(block)
        finalize! unless @disable_clear_and_finalize
        nil
@ilake
ilake / ruby load.md
Last active December 19, 2015 17:19