Rails 小技巧cheatsheet
Rails 小技巧
在 config/environments/development.rb:
RUBY# Source maps for Sassconfig.sass.debug_info = trueconfig.sass.line_comments = false# Don't break apartconfig.assets.debug = false
RUBY<%= render 'article', full: true %><%= render 'article' %><% if local_assigns[:full] %> ...<% end %>
i18n
YMLen: read_more_html: "read <b>more</b>..."
错误处理:
RUBY# config/application.rbconfig.exceptions_app = self.routesget '/404', to: 'errors#not_found'get '/500', to: 'errors#server_error'class ErrorsController def not_found render status: :not_found endend
Rails 更新:
SHELLrake rails:update
distinct/pluck:
RUBYArticle.distinct.pluck('author')
Relation#merge
RUBYscope :with_drafts, -> { uniq.joins(:articles).merge(Article.draft)}
order
RUBYscope :recent, -> { order created_at: :desc }
按月分组
RUBY.group("to_char(created_at, 'YYYY-MM')").group("to_char(created_at, 'YYYY-MM')").count