ruby && rails 小技巧

05 Jan 2015, by

.select('DISTINCT project_id')

` ActionController::Base.helpers.sanitize(self.description, tags: %w(li br)).gsub(/(<[^>]+>   \r)/,”\n”)`

"<<<www>>>,2, <<<fff>>>".gsub!(/<<<(.+?)>>>/) { $1 } #=> “www,2, fff”

where("name LIKE ?", "%#{query}%")

raw sanitize .html_safe sanitize(较安全)可以设置白名单

在routes.rb中加入

if Rails.env.development?
  ActiveSupport::Dependencies.explicitly_unloadable_constants << "API"

  api_files = Dir[Rails.root.join('app', 'api', '**', '*.rb')]
  api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do
    Rails.application.reload_routes!
  end
  ActionDispatch::Callbacks.to_prepare do
    api_reloader.execute_if_updated
  end
end

.select(&:persisted?)

compact